2 * Copyright 2012, The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "MediaCrypto-JNI"
19 #include <utils/Log.h>
21 #include "android_media_MediaCrypto.h"
23 #include "android_runtime/AndroidRuntime.h"
25 #include <nativehelper/JNIHelp.h>
27 #include <binder/IServiceManager.h>
28 #include <cutils/properties.h>
29 #include <media/ICrypto.h>
30 #include <media/IMediaDrmService.h>
31 #include <media/stagefright/foundation/ADebug.h>
39 static fields_t gFields;
41 static sp<JCrypto> getCrypto(JNIEnv *env, jobject thiz) {
42 return (JCrypto *)env->GetLongField(thiz, gFields.context);
46 JNIEnv *env, jobject thiz,
47 const uint8_t uuid[16], const void *initData, size_t initSize) {
48 mObject = env->NewWeakGlobalRef(thiz);
50 mCrypto = MakeCrypto(uuid, initData, initSize);
54 if (mCrypto != NULL) {
55 mCrypto->destroyPlugin();
59 JNIEnv *env = AndroidRuntime::getJNIEnv();
61 env->DeleteWeakGlobalRef(mObject);
66 sp<ICrypto> JCrypto::MakeCrypto() {
67 sp<IServiceManager> sm = defaultServiceManager();
69 sp<IBinder> binder = sm->getService(String16("media.drm"));
70 sp<IMediaDrmService> service = interface_cast<IMediaDrmService>(binder);
71 if (service == NULL) {
75 sp<ICrypto> crypto = service->makeCrypto();
76 if (crypto == NULL || (crypto->initCheck() != OK && crypto->initCheck() != NO_INIT)) {
84 sp<ICrypto> JCrypto::MakeCrypto(
85 const uint8_t uuid[16], const void *initData, size_t initSize) {
86 sp<ICrypto> crypto = MakeCrypto();
92 status_t err = crypto->createPlugin(uuid, initData, initSize);
101 bool JCrypto::requiresSecureDecoderComponent(const char *mime) const {
102 if (mCrypto == NULL) {
106 return mCrypto->requiresSecureDecoderComponent(mime);
110 bool JCrypto::IsCryptoSchemeSupported(const uint8_t uuid[16]) {
111 sp<ICrypto> crypto = MakeCrypto();
113 if (crypto == NULL) {
117 return crypto->isCryptoSchemeSupported(uuid);
120 status_t JCrypto::initCheck() const {
121 return mCrypto == NULL ? NO_INIT : OK;
125 sp<ICrypto> JCrypto::GetCrypto(JNIEnv *env, jobject obj) {
126 jclass clazz = env->FindClass("android/media/MediaCrypto");
127 CHECK(clazz != NULL);
129 if (!env->IsInstanceOf(obj, clazz)) {
133 sp<JCrypto> jcrypto = getCrypto(env, obj);
135 if (jcrypto == NULL) {
139 return jcrypto->mCrypto;
142 // JNI conversion utilities
143 static Vector<uint8_t> JByteArrayToVector(JNIEnv *env, jbyteArray const &byteArray) {
144 Vector<uint8_t> vector;
145 size_t length = env->GetArrayLength(byteArray);
146 vector.insertAt((size_t)0, length);
147 env->GetByteArrayRegion(byteArray, 0, length, (jbyte *)vector.editArray());
151 } // namespace android
153 using namespace android;
155 static sp<JCrypto> setCrypto(
156 JNIEnv *env, jobject thiz, const sp<JCrypto> &crypto) {
157 sp<JCrypto> old = (JCrypto *)env->GetLongField(thiz, gFields.context);
158 if (crypto != NULL) {
159 crypto->incStrong(thiz);
162 old->decStrong(thiz);
164 env->SetLongField(thiz, gFields.context, (jlong)crypto.get());
169 static void android_media_MediaCrypto_release(JNIEnv *env, jobject thiz) {
170 setCrypto(env, thiz, NULL);
173 static void android_media_MediaCrypto_native_init(JNIEnv *env) {
174 jclass clazz = env->FindClass("android/media/MediaCrypto");
175 CHECK(clazz != NULL);
177 gFields.context = env->GetFieldID(clazz, "mNativeContext", "J");
178 CHECK(gFields.context != NULL);
181 static void android_media_MediaCrypto_native_setup(
182 JNIEnv *env, jobject thiz,
183 jbyteArray uuidObj, jbyteArray initDataObj) {
184 jsize uuidLength = env->GetArrayLength(uuidObj);
186 if (uuidLength != 16) {
189 "java/lang/IllegalArgumentException",
195 jbyte *uuid = env->GetByteArrayElements(uuidObj, &isCopy);
197 jsize initDataLength = 0;
198 jbyte *initData = NULL;
200 if (initDataObj != NULL) {
201 initDataLength = env->GetArrayLength(initDataObj);
202 initData = env->GetByteArrayElements(initDataObj, &isCopy);
205 sp<JCrypto> crypto = new JCrypto(
206 env, thiz, (const uint8_t *)uuid, initData, initDataLength);
208 status_t err = crypto->initCheck();
210 if (initDataObj != NULL) {
211 env->ReleaseByteArrayElements(initDataObj, initData, 0);
215 env->ReleaseByteArrayElements(uuidObj, uuid, 0);
221 "android/media/MediaCryptoException",
222 "Failed to instantiate crypto object.");
226 setCrypto(env,thiz, crypto);
229 static void android_media_MediaCrypto_native_finalize(
230 JNIEnv *env, jobject thiz) {
231 android_media_MediaCrypto_release(env, thiz);
234 static jboolean android_media_MediaCrypto_isCryptoSchemeSupportedNative(
235 JNIEnv *env, jobject /* thiz */, jbyteArray uuidObj) {
236 jsize uuidLength = env->GetArrayLength(uuidObj);
238 if (uuidLength != 16) {
241 "java/lang/IllegalArgumentException",
247 jbyte *uuid = env->GetByteArrayElements(uuidObj, &isCopy);
249 bool result = JCrypto::IsCryptoSchemeSupported((const uint8_t *)uuid);
251 env->ReleaseByteArrayElements(uuidObj, uuid, 0);
254 return result ? JNI_TRUE : JNI_FALSE;
257 static jboolean android_media_MediaCrypto_requiresSecureDecoderComponent(
258 JNIEnv *env, jobject thiz, jstring mimeObj) {
259 if (mimeObj == NULL) {
260 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
264 sp<JCrypto> crypto = getCrypto(env, thiz);
266 if (crypto == NULL) {
267 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
271 const char *mime = env->GetStringUTFChars(mimeObj, NULL);
277 bool result = crypto->requiresSecureDecoderComponent(mime);
279 env->ReleaseStringUTFChars(mimeObj, mime);
282 return result ? JNI_TRUE : JNI_FALSE;
285 static void android_media_MediaCrypto_setMediaDrmSession(
286 JNIEnv *env, jobject thiz, jbyteArray jsessionId) {
287 if (jsessionId == NULL) {
288 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
292 sp<ICrypto> crypto = JCrypto::GetCrypto(env, thiz);
294 if (crypto == NULL) {
295 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
299 Vector<uint8_t> sessionId(JByteArrayToVector(env, jsessionId));
301 status_t err = crypto->setMediaDrmSession(sessionId);
304 String8 msg("setMediaDrmSession failed");
305 if (err == ERROR_DRM_SESSION_NOT_OPENED) {
306 msg += ": session not opened";
307 } else if (err == ERROR_UNSUPPORTED) {
308 msg += ": not supported by this crypto scheme";
309 } else if (err == NO_INIT) {
310 msg += ": crypto plugin not initialized";
312 msg.appendFormat(": general failure (%d)", err);
314 jniThrowException(env, "android/media/MediaCryptoException", msg.string());
318 static const JNINativeMethod gMethods[] = {
319 { "release", "()V", (void *)android_media_MediaCrypto_release },
320 { "native_init", "()V", (void *)android_media_MediaCrypto_native_init },
322 { "native_setup", "([B[B)V",
323 (void *)android_media_MediaCrypto_native_setup },
325 { "native_finalize", "()V",
326 (void *)android_media_MediaCrypto_native_finalize },
328 { "isCryptoSchemeSupportedNative", "([B)Z",
329 (void *)android_media_MediaCrypto_isCryptoSchemeSupportedNative },
331 { "requiresSecureDecoderComponent", "(Ljava/lang/String;)Z",
332 (void *)android_media_MediaCrypto_requiresSecureDecoderComponent },
334 { "setMediaDrmSession", "([B)V",
335 (void *)android_media_MediaCrypto_setMediaDrmSession },
338 int register_android_media_Crypto(JNIEnv *env) {
339 return AndroidRuntime::registerNativeMethods(env,
340 "android/media/MediaCrypto", gMethods, NELEM(gMethods));