blob: d2216fb97de4a8f8ab3cc193ee738b84b7c553ba [file] [log] [blame]
Andreas Huber8240d922012-04-04 14:06:32 -07001/*
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
Andreas Huber07ea4262012-04-11 12:21:20 -070018#define LOG_TAG "MediaCrypto-JNI"
Andreas Huber8240d922012-04-04 14:06:32 -070019#include <utils/Log.h>
20
Andreas Huber07ea4262012-04-11 12:21:20 -070021#include "android_media_MediaCrypto.h"
Andreas Huber8240d922012-04-04 14:06:32 -070022
23#include "android_runtime/AndroidRuntime.h"
24#include "jni.h"
25#include "JNIHelp.h"
26
27#include <binder/IServiceManager.h>
28#include <media/ICrypto.h>
29#include <media/IMediaPlayerService.h>
30#include <media/stagefright/foundation/ADebug.h>
31
32namespace android {
33
34struct fields_t {
35 jfieldID context;
36};
37
38static fields_t gFields;
39
40static sp<JCrypto> getCrypto(JNIEnv *env, jobject thiz) {
Ashok Bhat075e9a12014-01-06 13:45:09 +000041 return (JCrypto *)env->GetLongField(thiz, gFields.context);
Andreas Huber8240d922012-04-04 14:06:32 -070042}
43
44JCrypto::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
52JCrypto::~JCrypto() {
53 mCrypto.clear();
54
55 JNIEnv *env = AndroidRuntime::getJNIEnv();
56
57 env->DeleteWeakGlobalRef(mObject);
58 mObject = NULL;
59}
60
61// static
62sp<ICrypto> JCrypto::MakeCrypto() {
63 sp<IServiceManager> sm = defaultServiceManager();
64
65 sp<IBinder> binder =
66 sm->getService(String16("media.player"));
67
68 sp<IMediaPlayerService> service =
69 interface_cast<IMediaPlayerService>(binder);
70
71 if (service == NULL) {
72 return NULL;
73 }
74
75 sp<ICrypto> crypto = service->makeCrypto();
76
Jeff Tinker90defba2013-03-22 15:32:27 -070077 if (crypto == NULL || (crypto->initCheck() != OK && crypto->initCheck() != NO_INIT)) {
Andreas Huber8240d922012-04-04 14:06:32 -070078 return NULL;
79 }
80
81 return crypto;
82}
83
84// static
85sp<ICrypto> JCrypto::MakeCrypto(
86 const uint8_t uuid[16], const void *initData, size_t initSize) {
87 sp<ICrypto> crypto = MakeCrypto();
88
89 if (crypto == NULL) {
90 return NULL;
91 }
92
93 status_t err = crypto->createPlugin(uuid, initData, initSize);
94
95 if (err != OK) {
96 return NULL;
97 }
98
99 return crypto;
100}
101
102bool JCrypto::requiresSecureDecoderComponent(const char *mime) const {
103 if (mCrypto == NULL) {
104 return false;
105 }
106
107 return mCrypto->requiresSecureDecoderComponent(mime);
108}
109
110// static
111bool JCrypto::IsCryptoSchemeSupported(const uint8_t uuid[16]) {
112 sp<ICrypto> crypto = MakeCrypto();
113
114 if (crypto == NULL) {
115 return false;
116 }
117
118 return crypto->isCryptoSchemeSupported(uuid);
119}
120
121status_t JCrypto::initCheck() const {
122 return mCrypto == NULL ? NO_INIT : OK;
123}
124
125// static
126sp<ICrypto> JCrypto::GetCrypto(JNIEnv *env, jobject obj) {
Andreas Huber07ea4262012-04-11 12:21:20 -0700127 jclass clazz = env->FindClass("android/media/MediaCrypto");
Andreas Huber8240d922012-04-04 14:06:32 -0700128 CHECK(clazz != NULL);
129
130 if (!env->IsInstanceOf(obj, clazz)) {
131 return NULL;
132 }
133
134 sp<JCrypto> jcrypto = getCrypto(env, obj);
135
136 if (jcrypto == NULL) {
137 return NULL;
138 }
139
140 return jcrypto->mCrypto;
141}
142
143} // namespace android
144
145using namespace android;
146
147static sp<JCrypto> setCrypto(
148 JNIEnv *env, jobject thiz, const sp<JCrypto> &crypto) {
Ashok Bhat075e9a12014-01-06 13:45:09 +0000149 sp<JCrypto> old = (JCrypto *)env->GetLongField(thiz, gFields.context);
Andreas Huber8240d922012-04-04 14:06:32 -0700150 if (crypto != NULL) {
151 crypto->incStrong(thiz);
152 }
153 if (old != NULL) {
154 old->decStrong(thiz);
155 }
Ashok Bhat075e9a12014-01-06 13:45:09 +0000156 env->SetLongField(thiz, gFields.context, (jlong)crypto.get());
Andreas Huber8240d922012-04-04 14:06:32 -0700157
158 return old;
159}
160
Andreas Huber07ea4262012-04-11 12:21:20 -0700161static void android_media_MediaCrypto_release(JNIEnv *env, jobject thiz) {
Andreas Huber8240d922012-04-04 14:06:32 -0700162 setCrypto(env, thiz, NULL);
163}
164
Andreas Huber07ea4262012-04-11 12:21:20 -0700165static void android_media_MediaCrypto_native_init(JNIEnv *env) {
166 jclass clazz = env->FindClass("android/media/MediaCrypto");
Andreas Huber8240d922012-04-04 14:06:32 -0700167 CHECK(clazz != NULL);
168
Ashok Bhat075e9a12014-01-06 13:45:09 +0000169 gFields.context = env->GetFieldID(clazz, "mNativeContext", "J");
Andreas Huber8240d922012-04-04 14:06:32 -0700170 CHECK(gFields.context != NULL);
171}
172
Andreas Huber07ea4262012-04-11 12:21:20 -0700173static void android_media_MediaCrypto_native_setup(
Andreas Huber8240d922012-04-04 14:06:32 -0700174 JNIEnv *env, jobject thiz,
175 jbyteArray uuidObj, jbyteArray initDataObj) {
176 jsize uuidLength = env->GetArrayLength(uuidObj);
177
178 if (uuidLength != 16) {
179 jniThrowException(
180 env,
181 "java/lang/IllegalArgumentException",
182 NULL);
183 return;
184 }
185
186 jboolean isCopy;
187 jbyte *uuid = env->GetByteArrayElements(uuidObj, &isCopy);
188
Andreas Huber07ea4262012-04-11 12:21:20 -0700189 jsize initDataLength = 0;
190 jbyte *initData = NULL;
191
192 if (initDataObj != NULL) {
193 initDataLength = env->GetArrayLength(initDataObj);
194 initData = env->GetByteArrayElements(initDataObj, &isCopy);
195 }
Andreas Huber8240d922012-04-04 14:06:32 -0700196
197 sp<JCrypto> crypto = new JCrypto(
198 env, thiz, (const uint8_t *)uuid, initData, initDataLength);
199
200 status_t err = crypto->initCheck();
201
Andreas Huber07ea4262012-04-11 12:21:20 -0700202 if (initDataObj != NULL) {
203 env->ReleaseByteArrayElements(initDataObj, initData, 0);
204 initData = NULL;
205 }
Andreas Huber8240d922012-04-04 14:06:32 -0700206
207 env->ReleaseByteArrayElements(uuidObj, uuid, 0);
208 uuid = NULL;
209
210 if (err != OK) {
211 jniThrowException(
212 env,
Andreas Huber60d610b2012-05-02 16:06:09 -0700213 "android/media/MediaCryptoException",
Andreas Huber8240d922012-04-04 14:06:32 -0700214 "Failed to instantiate crypto object.");
215 return;
216 }
217
218 setCrypto(env,thiz, crypto);
219}
220
Andreas Huber07ea4262012-04-11 12:21:20 -0700221static void android_media_MediaCrypto_native_finalize(
Andreas Huber8240d922012-04-04 14:06:32 -0700222 JNIEnv *env, jobject thiz) {
Andreas Huber07ea4262012-04-11 12:21:20 -0700223 android_media_MediaCrypto_release(env, thiz);
Andreas Huber8240d922012-04-04 14:06:32 -0700224}
225
Andreas Huber60d610b2012-05-02 16:06:09 -0700226static jboolean android_media_MediaCrypto_isCryptoSchemeSupportedNative(
Andreas Gampe5a15d0d2014-11-10 18:19:40 -0800227 JNIEnv *env, jobject /* thiz */, jbyteArray uuidObj) {
Andreas Huber8240d922012-04-04 14:06:32 -0700228 jsize uuidLength = env->GetArrayLength(uuidObj);
229
230 if (uuidLength != 16) {
231 jniThrowException(
232 env,
233 "java/lang/IllegalArgumentException",
234 NULL);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000235 return JNI_FALSE;
Andreas Huber8240d922012-04-04 14:06:32 -0700236 }
237
238 jboolean isCopy;
239 jbyte *uuid = env->GetByteArrayElements(uuidObj, &isCopy);
240
241 bool result = JCrypto::IsCryptoSchemeSupported((const uint8_t *)uuid);
242
243 env->ReleaseByteArrayElements(uuidObj, uuid, 0);
244 uuid = NULL;
245
Ashok Bhat075e9a12014-01-06 13:45:09 +0000246 return result ? JNI_TRUE : JNI_FALSE;
Andreas Huber8240d922012-04-04 14:06:32 -0700247}
248
Andreas Huber07ea4262012-04-11 12:21:20 -0700249static jboolean android_media_MediaCrypto_requiresSecureDecoderComponent(
Andreas Huber8240d922012-04-04 14:06:32 -0700250 JNIEnv *env, jobject thiz, jstring mimeObj) {
251 if (mimeObj == NULL) {
252 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000253 return JNI_FALSE;
Andreas Huber8240d922012-04-04 14:06:32 -0700254 }
255
256 sp<JCrypto> crypto = getCrypto(env, thiz);
257
258 if (crypto == NULL) {
259 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000260 return JNI_FALSE;
Andreas Huber8240d922012-04-04 14:06:32 -0700261 }
262
263 const char *mime = env->GetStringUTFChars(mimeObj, NULL);
264
265 if (mime == NULL) {
Ashok Bhat075e9a12014-01-06 13:45:09 +0000266 return JNI_FALSE;
Andreas Huber8240d922012-04-04 14:06:32 -0700267 }
268
269 bool result = crypto->requiresSecureDecoderComponent(mime);
270
271 env->ReleaseStringUTFChars(mimeObj, mime);
272 mime = NULL;
273
Ashok Bhat075e9a12014-01-06 13:45:09 +0000274 return result ? JNI_TRUE : JNI_FALSE;
Andreas Huber8240d922012-04-04 14:06:32 -0700275}
276
277static JNINativeMethod gMethods[] = {
Andreas Huber07ea4262012-04-11 12:21:20 -0700278 { "release", "()V", (void *)android_media_MediaCrypto_release },
279 { "native_init", "()V", (void *)android_media_MediaCrypto_native_init },
Andreas Huber8240d922012-04-04 14:06:32 -0700280
281 { "native_setup", "([B[B)V",
Andreas Huber07ea4262012-04-11 12:21:20 -0700282 (void *)android_media_MediaCrypto_native_setup },
Andreas Huber8240d922012-04-04 14:06:32 -0700283
284 { "native_finalize", "()V",
Andreas Huber07ea4262012-04-11 12:21:20 -0700285 (void *)android_media_MediaCrypto_native_finalize },
Andreas Huber8240d922012-04-04 14:06:32 -0700286
Andreas Huber60d610b2012-05-02 16:06:09 -0700287 { "isCryptoSchemeSupportedNative", "([B)Z",
288 (void *)android_media_MediaCrypto_isCryptoSchemeSupportedNative },
Andreas Huber8240d922012-04-04 14:06:32 -0700289
290 { "requiresSecureDecoderComponent", "(Ljava/lang/String;)Z",
Andreas Huber07ea4262012-04-11 12:21:20 -0700291 (void *)android_media_MediaCrypto_requiresSecureDecoderComponent },
Andreas Huber8240d922012-04-04 14:06:32 -0700292};
293
294int register_android_media_Crypto(JNIEnv *env) {
295 return AndroidRuntime::registerNativeMethods(env,
Andreas Huber07ea4262012-04-11 12:21:20 -0700296 "android/media/MediaCrypto", gMethods, NELEM(gMethods));
Andreas Huber8240d922012-04-04 14:06:32 -0700297}
298