blob: 2d9051f5230dbc00a9444e134fa0bdd0f23c1f32 [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"
Steven Moreland2279b252017-07-19 09:50:45 -070025#include <nativehelper/JNIHelp.h>
Andreas Huber8240d922012-04-04 14:06:32 -070026
27#include <binder/IServiceManager.h>
Jeff Tinkerdc614f82016-02-12 08:58:32 -080028#include <cutils/properties.h>
Andreas Huber8240d922012-04-04 14:06:32 -070029#include <media/stagefright/foundation/ADebug.h>
Jeff Tinkercd4d28f2018-02-16 16:24:49 -080030#include <mediadrm/ICrypto.h>
31#include <mediadrm/IMediaDrmService.h>
Andreas Huber8240d922012-04-04 14:06:32 -070032
33namespace android {
34
35struct fields_t {
36 jfieldID context;
37};
38
39static fields_t gFields;
40
41static sp<JCrypto> getCrypto(JNIEnv *env, jobject thiz) {
Ashok Bhat075e9a12014-01-06 13:45:09 +000042 return (JCrypto *)env->GetLongField(thiz, gFields.context);
Andreas Huber8240d922012-04-04 14:06:32 -070043}
44
45JCrypto::JCrypto(
46 JNIEnv *env, jobject thiz,
47 const uint8_t uuid[16], const void *initData, size_t initSize) {
48 mObject = env->NewWeakGlobalRef(thiz);
49
50 mCrypto = MakeCrypto(uuid, initData, initSize);
51}
52
53JCrypto::~JCrypto() {
Jeff Tinker94dd36c2017-05-08 19:22:06 -070054 if (mCrypto != NULL) {
55 mCrypto->destroyPlugin();
56 }
Andreas Huber8240d922012-04-04 14:06:32 -070057 mCrypto.clear();
58
59 JNIEnv *env = AndroidRuntime::getJNIEnv();
60
61 env->DeleteWeakGlobalRef(mObject);
62 mObject = NULL;
63}
64
65// static
66sp<ICrypto> JCrypto::MakeCrypto() {
67 sp<IServiceManager> sm = defaultServiceManager();
68
Jeff Tinkerd12b7c02016-04-22 17:50:33 -070069 sp<IBinder> binder = sm->getService(String16("media.drm"));
70 sp<IMediaDrmService> service = interface_cast<IMediaDrmService>(binder);
71 if (service == NULL) {
72 return NULL;
Andreas Huber8240d922012-04-04 14:06:32 -070073 }
74
Jeff Tinkerd12b7c02016-04-22 17:50:33 -070075 sp<ICrypto> crypto = service->makeCrypto();
Jeff Tinker90defba2013-03-22 15:32:27 -070076 if (crypto == NULL || (crypto->initCheck() != OK && crypto->initCheck() != NO_INIT)) {
Andreas Huber8240d922012-04-04 14:06:32 -070077 return NULL;
78 }
79
80 return crypto;
81}
82
83// static
84sp<ICrypto> JCrypto::MakeCrypto(
85 const uint8_t uuid[16], const void *initData, size_t initSize) {
86 sp<ICrypto> crypto = MakeCrypto();
87
88 if (crypto == NULL) {
89 return NULL;
90 }
91
92 status_t err = crypto->createPlugin(uuid, initData, initSize);
93
94 if (err != OK) {
95 return NULL;
96 }
97
98 return crypto;
99}
100
101bool JCrypto::requiresSecureDecoderComponent(const char *mime) const {
102 if (mCrypto == NULL) {
103 return false;
104 }
105
106 return mCrypto->requiresSecureDecoderComponent(mime);
107}
108
109// static
110bool JCrypto::IsCryptoSchemeSupported(const uint8_t uuid[16]) {
111 sp<ICrypto> crypto = MakeCrypto();
112
113 if (crypto == NULL) {
114 return false;
115 }
116
117 return crypto->isCryptoSchemeSupported(uuid);
118}
119
120status_t JCrypto::initCheck() const {
121 return mCrypto == NULL ? NO_INIT : OK;
122}
123
124// static
125sp<ICrypto> JCrypto::GetCrypto(JNIEnv *env, jobject obj) {
Andreas Huber07ea4262012-04-11 12:21:20 -0700126 jclass clazz = env->FindClass("android/media/MediaCrypto");
Andreas Huber8240d922012-04-04 14:06:32 -0700127 CHECK(clazz != NULL);
128
129 if (!env->IsInstanceOf(obj, clazz)) {
130 return NULL;
131 }
132
133 sp<JCrypto> jcrypto = getCrypto(env, obj);
134
135 if (jcrypto == NULL) {
136 return NULL;
137 }
138
139 return jcrypto->mCrypto;
140}
141
Jeff Tinker6f6ef112015-04-10 04:05:25 -0700142// JNI conversion utilities
143static 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());
148 return vector;
149}
150
Andreas Huber8240d922012-04-04 14:06:32 -0700151} // namespace android
152
153using namespace android;
154
155static sp<JCrypto> setCrypto(
156 JNIEnv *env, jobject thiz, const sp<JCrypto> &crypto) {
Ashok Bhat075e9a12014-01-06 13:45:09 +0000157 sp<JCrypto> old = (JCrypto *)env->GetLongField(thiz, gFields.context);
Andreas Huber8240d922012-04-04 14:06:32 -0700158 if (crypto != NULL) {
159 crypto->incStrong(thiz);
160 }
161 if (old != NULL) {
162 old->decStrong(thiz);
163 }
Ashok Bhat075e9a12014-01-06 13:45:09 +0000164 env->SetLongField(thiz, gFields.context, (jlong)crypto.get());
Andreas Huber8240d922012-04-04 14:06:32 -0700165
166 return old;
167}
168
Andreas Huber07ea4262012-04-11 12:21:20 -0700169static void android_media_MediaCrypto_release(JNIEnv *env, jobject thiz) {
Andreas Huber8240d922012-04-04 14:06:32 -0700170 setCrypto(env, thiz, NULL);
171}
172
Andreas Huber07ea4262012-04-11 12:21:20 -0700173static void android_media_MediaCrypto_native_init(JNIEnv *env) {
174 jclass clazz = env->FindClass("android/media/MediaCrypto");
Andreas Huber8240d922012-04-04 14:06:32 -0700175 CHECK(clazz != NULL);
176
Ashok Bhat075e9a12014-01-06 13:45:09 +0000177 gFields.context = env->GetFieldID(clazz, "mNativeContext", "J");
Andreas Huber8240d922012-04-04 14:06:32 -0700178 CHECK(gFields.context != NULL);
179}
180
Andreas Huber07ea4262012-04-11 12:21:20 -0700181static void android_media_MediaCrypto_native_setup(
Andreas Huber8240d922012-04-04 14:06:32 -0700182 JNIEnv *env, jobject thiz,
183 jbyteArray uuidObj, jbyteArray initDataObj) {
184 jsize uuidLength = env->GetArrayLength(uuidObj);
185
186 if (uuidLength != 16) {
187 jniThrowException(
188 env,
189 "java/lang/IllegalArgumentException",
190 NULL);
191 return;
192 }
193
194 jboolean isCopy;
195 jbyte *uuid = env->GetByteArrayElements(uuidObj, &isCopy);
196
Andreas Huber07ea4262012-04-11 12:21:20 -0700197 jsize initDataLength = 0;
198 jbyte *initData = NULL;
199
200 if (initDataObj != NULL) {
201 initDataLength = env->GetArrayLength(initDataObj);
202 initData = env->GetByteArrayElements(initDataObj, &isCopy);
203 }
Andreas Huber8240d922012-04-04 14:06:32 -0700204
205 sp<JCrypto> crypto = new JCrypto(
206 env, thiz, (const uint8_t *)uuid, initData, initDataLength);
207
208 status_t err = crypto->initCheck();
209
Andreas Huber07ea4262012-04-11 12:21:20 -0700210 if (initDataObj != NULL) {
211 env->ReleaseByteArrayElements(initDataObj, initData, 0);
212 initData = NULL;
213 }
Andreas Huber8240d922012-04-04 14:06:32 -0700214
215 env->ReleaseByteArrayElements(uuidObj, uuid, 0);
216 uuid = NULL;
217
218 if (err != OK) {
219 jniThrowException(
220 env,
Andreas Huber60d610b2012-05-02 16:06:09 -0700221 "android/media/MediaCryptoException",
Andreas Huber8240d922012-04-04 14:06:32 -0700222 "Failed to instantiate crypto object.");
223 return;
224 }
225
226 setCrypto(env,thiz, crypto);
227}
228
Andreas Huber07ea4262012-04-11 12:21:20 -0700229static void android_media_MediaCrypto_native_finalize(
Andreas Huber8240d922012-04-04 14:06:32 -0700230 JNIEnv *env, jobject thiz) {
Andreas Huber07ea4262012-04-11 12:21:20 -0700231 android_media_MediaCrypto_release(env, thiz);
Andreas Huber8240d922012-04-04 14:06:32 -0700232}
233
Andreas Huber60d610b2012-05-02 16:06:09 -0700234static jboolean android_media_MediaCrypto_isCryptoSchemeSupportedNative(
Andreas Gampe5a15d0d2014-11-10 18:19:40 -0800235 JNIEnv *env, jobject /* thiz */, jbyteArray uuidObj) {
Andreas Huber8240d922012-04-04 14:06:32 -0700236 jsize uuidLength = env->GetArrayLength(uuidObj);
237
238 if (uuidLength != 16) {
239 jniThrowException(
240 env,
241 "java/lang/IllegalArgumentException",
242 NULL);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000243 return JNI_FALSE;
Andreas Huber8240d922012-04-04 14:06:32 -0700244 }
245
246 jboolean isCopy;
247 jbyte *uuid = env->GetByteArrayElements(uuidObj, &isCopy);
248
249 bool result = JCrypto::IsCryptoSchemeSupported((const uint8_t *)uuid);
250
251 env->ReleaseByteArrayElements(uuidObj, uuid, 0);
252 uuid = NULL;
253
Ashok Bhat075e9a12014-01-06 13:45:09 +0000254 return result ? JNI_TRUE : JNI_FALSE;
Andreas Huber8240d922012-04-04 14:06:32 -0700255}
256
Andreas Huber07ea4262012-04-11 12:21:20 -0700257static jboolean android_media_MediaCrypto_requiresSecureDecoderComponent(
Andreas Huber8240d922012-04-04 14:06:32 -0700258 JNIEnv *env, jobject thiz, jstring mimeObj) {
259 if (mimeObj == NULL) {
260 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000261 return JNI_FALSE;
Andreas Huber8240d922012-04-04 14:06:32 -0700262 }
263
264 sp<JCrypto> crypto = getCrypto(env, thiz);
265
266 if (crypto == NULL) {
267 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000268 return JNI_FALSE;
Andreas Huber8240d922012-04-04 14:06:32 -0700269 }
270
271 const char *mime = env->GetStringUTFChars(mimeObj, NULL);
272
273 if (mime == NULL) {
Ashok Bhat075e9a12014-01-06 13:45:09 +0000274 return JNI_FALSE;
Andreas Huber8240d922012-04-04 14:06:32 -0700275 }
276
277 bool result = crypto->requiresSecureDecoderComponent(mime);
278
279 env->ReleaseStringUTFChars(mimeObj, mime);
280 mime = NULL;
281
Ashok Bhat075e9a12014-01-06 13:45:09 +0000282 return result ? JNI_TRUE : JNI_FALSE;
Andreas Huber8240d922012-04-04 14:06:32 -0700283}
284
Jeff Tinker6f6ef112015-04-10 04:05:25 -0700285static void android_media_MediaCrypto_setMediaDrmSession(
286 JNIEnv *env, jobject thiz, jbyteArray jsessionId) {
287 if (jsessionId == NULL) {
288 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
289 return;
290 }
291
292 sp<ICrypto> crypto = JCrypto::GetCrypto(env, thiz);
293
294 if (crypto == NULL) {
295 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
296 return;
297 }
298
299 Vector<uint8_t> sessionId(JByteArrayToVector(env, jsessionId));
300
301 status_t err = crypto->setMediaDrmSession(sessionId);
302
Jeff Tinker025af8b2015-06-01 14:17:06 -0700303 if (err != OK) {
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";
311 } else {
312 msg.appendFormat(": general failure (%d)", err);
313 }
314 jniThrowException(env, "android/media/MediaCryptoException", msg.string());
Jeff Tinker6f6ef112015-04-10 04:05:25 -0700315 }
Jeff Tinker6f6ef112015-04-10 04:05:25 -0700316}
317
Daniel Micay76f6a862015-09-19 17:31:01 -0400318static const JNINativeMethod gMethods[] = {
Andreas Huber07ea4262012-04-11 12:21:20 -0700319 { "release", "()V", (void *)android_media_MediaCrypto_release },
320 { "native_init", "()V", (void *)android_media_MediaCrypto_native_init },
Andreas Huber8240d922012-04-04 14:06:32 -0700321
322 { "native_setup", "([B[B)V",
Andreas Huber07ea4262012-04-11 12:21:20 -0700323 (void *)android_media_MediaCrypto_native_setup },
Andreas Huber8240d922012-04-04 14:06:32 -0700324
325 { "native_finalize", "()V",
Andreas Huber07ea4262012-04-11 12:21:20 -0700326 (void *)android_media_MediaCrypto_native_finalize },
Andreas Huber8240d922012-04-04 14:06:32 -0700327
Andreas Huber60d610b2012-05-02 16:06:09 -0700328 { "isCryptoSchemeSupportedNative", "([B)Z",
329 (void *)android_media_MediaCrypto_isCryptoSchemeSupportedNative },
Andreas Huber8240d922012-04-04 14:06:32 -0700330
331 { "requiresSecureDecoderComponent", "(Ljava/lang/String;)Z",
Andreas Huber07ea4262012-04-11 12:21:20 -0700332 (void *)android_media_MediaCrypto_requiresSecureDecoderComponent },
Jeff Tinker6f6ef112015-04-10 04:05:25 -0700333
334 { "setMediaDrmSession", "([B)V",
335 (void *)android_media_MediaCrypto_setMediaDrmSession },
Andreas Huber8240d922012-04-04 14:06:32 -0700336};
337
338int register_android_media_Crypto(JNIEnv *env) {
339 return AndroidRuntime::registerNativeMethods(env,
Andreas Huber07ea4262012-04-11 12:21:20 -0700340 "android/media/MediaCrypto", gMethods, NELEM(gMethods));
Andreas Huber8240d922012-04-04 14:06:32 -0700341}
342