blob: f930a03c1c1a3be950dfe9c125d7a34c1b51f4f7 [file] [log] [blame]
James Dongf3997522011-03-11 12:02:12 -08001/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
James Dongf3997522011-03-11 12:02:12 -080018//#define LOG_NDEBUG 0
19#define LOG_TAG "MediaScannerJNI"
20#include <utils/Log.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021#include <utils/threads.h>
Homin Lee1a8b6c22012-04-05 20:11:27 +090022#include <utils/Unicode.h>
James Dongf3997522011-03-11 12:02:12 -080023#include <media/mediascanner.h>
24#include <media/stagefright/StagefrightMediaScanner.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025
26#include "jni.h"
27#include "JNIHelp.h"
28#include "android_runtime/AndroidRuntime.h"
29
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030using namespace android;
31
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
James Dongf3997522011-03-11 12:02:12 -080033static const char* const kClassMediaScannerClient =
34 "android/media/MediaScannerClient";
35
36static const char* const kClassMediaScanner =
37 "android/media/MediaScanner";
38
39static const char* const kRunTimeException =
40 "java/lang/RuntimeException";
41
42static const char* const kIllegalArgumentException =
43 "java/lang/IllegalArgumentException";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
James Dong133cf8b2011-03-11 15:18:40 -080045struct fields_t {
46 jfieldID context;
47};
48static fields_t fields;
James Dong133cf8b2011-03-11 15:18:40 -080049
Jeff Brown2c70d4a2011-07-20 16:38:43 -070050static status_t checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
51 if (env->ExceptionCheck()) {
Steve Blockc6aacce2012-01-06 19:20:56 +000052 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown2c70d4a2011-07-20 16:38:43 -070053 LOGE_EX(env);
54 env->ExceptionClear();
55 return UNKNOWN_ERROR;
56 }
57 return OK;
58}
59
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060class MyMediaScannerClient : public MediaScannerClient
61{
62public:
63 MyMediaScannerClient(JNIEnv *env, jobject client)
64 : mEnv(env),
65 mClient(env->NewGlobalRef(client)),
66 mScanFileMethodID(0),
67 mHandleStringTagMethodID(0),
68 mSetMimeTypeMethodID(0)
69 {
Steve Block06ade6a2011-10-20 11:56:00 +010070 ALOGV("MyMediaScannerClient constructor");
James Dongf3997522011-03-11 12:02:12 -080071 jclass mediaScannerClientInterface =
72 env->FindClass(kClassMediaScannerClient);
73
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 if (mediaScannerClientInterface == NULL) {
Steve Blockc6aacce2012-01-06 19:20:56 +000075 ALOGE("Class %s not found", kClassMediaScannerClient);
James Dongf3997522011-03-11 12:02:12 -080076 } else {
77 mScanFileMethodID = env->GetMethodID(
78 mediaScannerClientInterface,
79 "scanFile",
Mike Lockwood997354e2011-04-24 11:15:09 -070080 "(Ljava/lang/String;JJZZ)V");
James Dongf3997522011-03-11 12:02:12 -080081
82 mHandleStringTagMethodID = env->GetMethodID(
83 mediaScannerClientInterface,
84 "handleStringTag",
85 "(Ljava/lang/String;Ljava/lang/String;)V");
86
87 mSetMimeTypeMethodID = env->GetMethodID(
88 mediaScannerClientInterface,
89 "setMimeType",
90 "(Ljava/lang/String;)V");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 }
92 }
James Dongf3997522011-03-11 12:02:12 -080093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 virtual ~MyMediaScannerClient()
95 {
Steve Block06ade6a2011-10-20 11:56:00 +010096 ALOGV("MyMediaScannerClient destructor");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 mEnv->DeleteGlobalRef(mClient);
98 }
James Dongf3997522011-03-11 12:02:12 -080099
Jeff Brown2c70d4a2011-07-20 16:38:43 -0700100 virtual status_t scanFile(const char* path, long long lastModified,
Mike Lockwood997354e2011-04-24 11:15:09 -0700101 long long fileSize, bool isDirectory, bool noMedia)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 {
Steve Block06ade6a2011-10-20 11:56:00 +0100103 ALOGV("scanFile: path(%s), time(%lld), size(%lld) and isDir(%d)",
James Dongf3997522011-03-11 12:02:12 -0800104 path, lastModified, fileSize, isDirectory);
105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 jstring pathStr;
James Dongf3997522011-03-11 12:02:12 -0800107 if ((pathStr = mEnv->NewStringUTF(path)) == NULL) {
Jeff Brown2c70d4a2011-07-20 16:38:43 -0700108 mEnv->ExceptionClear();
109 return NO_MEMORY;
James Dongf3997522011-03-11 12:02:12 -0800110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111
Mike Lockwood076e05b2010-12-16 12:54:24 -0800112 mEnv->CallVoidMethod(mClient, mScanFileMethodID, pathStr, lastModified,
Mike Lockwood997354e2011-04-24 11:15:09 -0700113 fileSize, isDirectory, noMedia);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114
115 mEnv->DeleteLocalRef(pathStr);
Jeff Brown2c70d4a2011-07-20 16:38:43 -0700116 return checkAndClearExceptionFromCallback(mEnv, "scanFile");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 }
118
Jeff Brown2c70d4a2011-07-20 16:38:43 -0700119 virtual status_t handleStringTag(const char* name, const char* value)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 {
Steve Block06ade6a2011-10-20 11:56:00 +0100121 ALOGV("handleStringTag: name(%s) and value(%s)", name, value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 jstring nameStr, valueStr;
James Dongf3997522011-03-11 12:02:12 -0800123 if ((nameStr = mEnv->NewStringUTF(name)) == NULL) {
Jeff Brown2c70d4a2011-07-20 16:38:43 -0700124 mEnv->ExceptionClear();
125 return NO_MEMORY;
James Dongf3997522011-03-11 12:02:12 -0800126 }
Homin Lee1a8b6c22012-04-05 20:11:27 +0900127
128 // Check if the value is valid UTF-8 string and replace
129 // any un-printable characters with '?' when it's not.
Marco Nelissenf51f1bd2011-11-02 10:49:50 -0700130 char *cleaned = NULL;
Homin Lee1a8b6c22012-04-05 20:11:27 +0900131 if (utf8_length(value) == -1) {
Marco Nelissenf51f1bd2011-11-02 10:49:50 -0700132 cleaned = strdup(value);
133 char *chp = cleaned;
134 char ch;
135 while ((ch = *chp)) {
136 if (ch & 0x80) {
137 *chp = '?';
138 }
139 chp++;
140 }
141 value = cleaned;
142 }
143 valueStr = mEnv->NewStringUTF(value);
144 free(cleaned);
145 if (valueStr == NULL) {
Jeff Brown2c70d4a2011-07-20 16:38:43 -0700146 mEnv->DeleteLocalRef(nameStr);
147 mEnv->ExceptionClear();
148 return NO_MEMORY;
James Dongf3997522011-03-11 12:02:12 -0800149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150
James Dongf3997522011-03-11 12:02:12 -0800151 mEnv->CallVoidMethod(
152 mClient, mHandleStringTagMethodID, nameStr, valueStr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153
154 mEnv->DeleteLocalRef(nameStr);
155 mEnv->DeleteLocalRef(valueStr);
Jeff Brown2c70d4a2011-07-20 16:38:43 -0700156 return checkAndClearExceptionFromCallback(mEnv, "handleStringTag");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 }
158
Jeff Brown2c70d4a2011-07-20 16:38:43 -0700159 virtual status_t setMimeType(const char* mimeType)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 {
Steve Block06ade6a2011-10-20 11:56:00 +0100161 ALOGV("setMimeType: %s", mimeType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 jstring mimeTypeStr;
James Dongf3997522011-03-11 12:02:12 -0800163 if ((mimeTypeStr = mEnv->NewStringUTF(mimeType)) == NULL) {
Jeff Brown2c70d4a2011-07-20 16:38:43 -0700164 mEnv->ExceptionClear();
165 return NO_MEMORY;
James Dongf3997522011-03-11 12:02:12 -0800166 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167
168 mEnv->CallVoidMethod(mClient, mSetMimeTypeMethodID, mimeTypeStr);
169
170 mEnv->DeleteLocalRef(mimeTypeStr);
Jeff Brown2c70d4a2011-07-20 16:38:43 -0700171 return checkAndClearExceptionFromCallback(mEnv, "setMimeType");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 }
173
174private:
175 JNIEnv *mEnv;
176 jobject mClient;
James Dongf3997522011-03-11 12:02:12 -0800177 jmethodID mScanFileMethodID;
178 jmethodID mHandleStringTagMethodID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 jmethodID mSetMimeTypeMethodID;
180};
181
182
James Dong133cf8b2011-03-11 15:18:40 -0800183static MediaScanner *getNativeScanner_l(JNIEnv* env, jobject thiz)
184{
185 return (MediaScanner *) env->GetIntField(thiz, fields.context);
186}
187
James Dong133cf8b2011-03-11 15:18:40 -0800188static void setNativeScanner_l(JNIEnv* env, jobject thiz, MediaScanner *s)
189{
190 env->SetIntField(thiz, fields.context, (int)s);
191}
192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193static void
James Dongf3997522011-03-11 12:02:12 -0800194android_media_MediaScanner_processDirectory(
195 JNIEnv *env, jobject thiz, jstring path, jobject client)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196{
Steve Block06ade6a2011-10-20 11:56:00 +0100197 ALOGV("processDirectory");
James Dong133cf8b2011-03-11 15:18:40 -0800198 MediaScanner *mp = getNativeScanner_l(env, thiz);
199 if (mp == NULL) {
200 jniThrowException(env, kRunTimeException, "No scanner available");
201 return;
202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203
204 if (path == NULL) {
James Dongf3997522011-03-11 12:02:12 -0800205 jniThrowException(env, kIllegalArgumentException, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 return;
207 }
Mike Lockwoodc37255d2010-09-10 14:47:36 -0400208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 const char *pathStr = env->GetStringUTFChars(path, NULL);
210 if (pathStr == NULL) { // Out of memory
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 return;
212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213
214 MyMediaScannerClient myClient(env, client);
Jeff Brown2c70d4a2011-07-20 16:38:43 -0700215 MediaScanResult result = mp->processDirectory(pathStr, myClient);
216 if (result == MEDIA_SCAN_RESULT_ERROR) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000217 ALOGE("An error occurred while scanning directory '%s'.", pathStr);
Jeff Brown2c70d4a2011-07-20 16:38:43 -0700218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 env->ReleaseStringUTFChars(path, pathStr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220}
221
222static void
James Dongf3997522011-03-11 12:02:12 -0800223android_media_MediaScanner_processFile(
224 JNIEnv *env, jobject thiz, jstring path,
225 jstring mimeType, jobject client)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226{
Steve Block06ade6a2011-10-20 11:56:00 +0100227 ALOGV("processFile");
James Dong133cf8b2011-03-11 15:18:40 -0800228
229 // Lock already hold by processDirectory
230 MediaScanner *mp = getNativeScanner_l(env, thiz);
231 if (mp == NULL) {
232 jniThrowException(env, kRunTimeException, "No scanner available");
233 return;
234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235
236 if (path == NULL) {
James Dongf3997522011-03-11 12:02:12 -0800237 jniThrowException(env, kIllegalArgumentException, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 return;
239 }
James Dongf3997522011-03-11 12:02:12 -0800240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 const char *pathStr = env->GetStringUTFChars(path, NULL);
242 if (pathStr == NULL) { // Out of memory
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 return;
244 }
James Dongf3997522011-03-11 12:02:12 -0800245
246 const char *mimeTypeStr =
247 (mimeType ? env->GetStringUTFChars(mimeType, NULL) : NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 if (mimeType && mimeTypeStr == NULL) { // Out of memory
James Dongc371a022011-04-06 12:16:07 -0700249 // ReleaseStringUTFChars can be called with an exception pending.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 env->ReleaseStringUTFChars(path, pathStr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 return;
252 }
253
254 MyMediaScannerClient myClient(env, client);
Jeff Brown2c70d4a2011-07-20 16:38:43 -0700255 MediaScanResult result = mp->processFile(pathStr, mimeTypeStr, myClient);
256 if (result == MEDIA_SCAN_RESULT_ERROR) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000257 ALOGE("An error occurred while scanning file '%s'.", pathStr);
Jeff Brown2c70d4a2011-07-20 16:38:43 -0700258 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 env->ReleaseStringUTFChars(path, pathStr);
260 if (mimeType) {
261 env->ReleaseStringUTFChars(mimeType, mimeTypeStr);
262 }
263}
264
265static void
James Dongf3997522011-03-11 12:02:12 -0800266android_media_MediaScanner_setLocale(
267 JNIEnv *env, jobject thiz, jstring locale)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268{
Steve Block06ade6a2011-10-20 11:56:00 +0100269 ALOGV("setLocale");
James Dong133cf8b2011-03-11 15:18:40 -0800270 MediaScanner *mp = getNativeScanner_l(env, thiz);
271 if (mp == NULL) {
272 jniThrowException(env, kRunTimeException, "No scanner available");
273 return;
274 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275
276 if (locale == NULL) {
James Dongf3997522011-03-11 12:02:12 -0800277 jniThrowException(env, kIllegalArgumentException, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 return;
279 }
280 const char *localeStr = env->GetStringUTFChars(locale, NULL);
281 if (localeStr == NULL) { // Out of memory
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 return;
283 }
284 mp->setLocale(localeStr);
285
286 env->ReleaseStringUTFChars(locale, localeStr);
287}
288
289static jbyteArray
James Dongf3997522011-03-11 12:02:12 -0800290android_media_MediaScanner_extractAlbumArt(
291 JNIEnv *env, jobject thiz, jobject fileDescriptor)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292{
Steve Block06ade6a2011-10-20 11:56:00 +0100293 ALOGV("extractAlbumArt");
James Dong133cf8b2011-03-11 15:18:40 -0800294 MediaScanner *mp = getNativeScanner_l(env, thiz);
295 if (mp == NULL) {
296 jniThrowException(env, kRunTimeException, "No scanner available");
297 return NULL;
298 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299
300 if (fileDescriptor == NULL) {
James Dongf3997522011-03-11 12:02:12 -0800301 jniThrowException(env, kIllegalArgumentException, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 return NULL;
303 }
304
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700305 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 char* data = mp->extractAlbumArt(fd);
307 if (!data) {
308 return NULL;
309 }
310 long len = *((long*)data);
James Dongf3997522011-03-11 12:02:12 -0800311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 jbyteArray array = env->NewByteArray(len);
313 if (array != NULL) {
314 jbyte* bytes = env->GetByteArrayElements(array, NULL);
315 memcpy(bytes, data + 4, len);
316 env->ReleaseByteArrayElements(array, bytes, 0);
317 }
James Dongf3997522011-03-11 12:02:12 -0800318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319done:
320 free(data);
321 // if NewByteArray() returned NULL, an out-of-memory
322 // exception will have been raised. I just want to
323 // return null in that case.
324 env->ExceptionClear();
325 return array;
326}
327
Marco Nelissen4935d052009-08-03 11:12:58 -0700328// This function gets a field ID, which in turn causes class initialization.
329// It is called from a static block in MediaScanner, which won't run until the
330// first time an instance of this class is used.
331static void
332android_media_MediaScanner_native_init(JNIEnv *env)
333{
Steve Block06ade6a2011-10-20 11:56:00 +0100334 ALOGV("native_init");
James Dongf3997522011-03-11 12:02:12 -0800335 jclass clazz = env->FindClass(kClassMediaScanner);
Marco Nelissen4935d052009-08-03 11:12:58 -0700336 if (clazz == NULL) {
Marco Nelissen4935d052009-08-03 11:12:58 -0700337 return;
338 }
339
340 fields.context = env->GetFieldID(clazz, "mNativeContext", "I");
341 if (fields.context == NULL) {
Marco Nelissen4935d052009-08-03 11:12:58 -0700342 return;
343 }
344}
345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346static void
347android_media_MediaScanner_native_setup(JNIEnv *env, jobject thiz)
348{
Steve Block06ade6a2011-10-20 11:56:00 +0100349 ALOGV("native_setup");
Andreas Huber8d65dd22010-06-23 16:40:57 -0700350 MediaScanner *mp = new StagefrightMediaScanner;
Andreas Huberbfb9fb12009-12-03 11:31:19 -0800351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 if (mp == NULL) {
James Dongf3997522011-03-11 12:02:12 -0800353 jniThrowException(env, kRunTimeException, "Out of memory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 return;
355 }
356
357 env->SetIntField(thiz, fields.context, (int)mp);
358}
359
360static void
361android_media_MediaScanner_native_finalize(JNIEnv *env, jobject thiz)
362{
Steve Block06ade6a2011-10-20 11:56:00 +0100363 ALOGV("native_finalize");
James Dong133cf8b2011-03-11 15:18:40 -0800364 MediaScanner *mp = getNativeScanner_l(env, thiz);
James Dongf3997522011-03-11 12:02:12 -0800365 if (mp == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 return;
James Dongf3997522011-03-11 12:02:12 -0800367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 delete mp;
James Dong133cf8b2011-03-11 15:18:40 -0800369 setNativeScanner_l(env, thiz, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370}
371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372static JNINativeMethod gMethods[] = {
James Dongf3997522011-03-11 12:02:12 -0800373 {
374 "processDirectory",
375 "(Ljava/lang/String;Landroid/media/MediaScannerClient;)V",
376 (void *)android_media_MediaScanner_processDirectory
377 },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378
James Dongf3997522011-03-11 12:02:12 -0800379 {
380 "processFile",
381 "(Ljava/lang/String;Ljava/lang/String;Landroid/media/MediaScannerClient;)V",
382 (void *)android_media_MediaScanner_processFile
383 },
384
385 {
386 "setLocale",
387 "(Ljava/lang/String;)V",
388 (void *)android_media_MediaScanner_setLocale
389 },
390
391 {
392 "extractAlbumArt",
393 "(Ljava/io/FileDescriptor;)[B",
394 (void *)android_media_MediaScanner_extractAlbumArt
395 },
396
397 {
398 "native_init",
399 "()V",
400 (void *)android_media_MediaScanner_native_init
401 },
402
403 {
404 "native_setup",
405 "()V",
406 (void *)android_media_MediaScanner_native_setup
407 },
408
409 {
410 "native_finalize",
411 "()V",
412 (void *)android_media_MediaScanner_native_finalize
413 },
414};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415
Marco Nelissen4935d052009-08-03 11:12:58 -0700416// This function only registers the native methods, and is called from
417// JNI_OnLoad in android_media_MediaPlayer.cpp
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418int register_android_media_MediaScanner(JNIEnv *env)
419{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 return AndroidRuntime::registerNativeMethods(env,
James Dongf3997522011-03-11 12:02:12 -0800421 kClassMediaScanner, gMethods, NELEM(gMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422}