3 ** Copyright 2007, The Android Open Source Project
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
9 ** http://www.apache.org/licenses/LICENSE-2.0
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.
18 //#define LOG_NDEBUG 0
19 #define LOG_TAG "MediaScannerJNI"
20 #include <utils/Log.h>
21 #include <utils/threads.h>
22 #include <media/mediascanner.h>
23 #include <media/stagefright/StagefrightMediaScanner.h>
24 #include <private/media/VideoFrame.h>
28 #include "android_runtime/AndroidRuntime.h"
29 #include "android_runtime/Log.h"
31 using namespace android;
34 static const char* const kClassMediaScannerClient =
35 "android/media/MediaScannerClient";
37 static const char* const kClassMediaScanner =
38 "android/media/MediaScanner";
40 static const char* const kRunTimeException =
41 "java/lang/RuntimeException";
43 static const char* const kIllegalArgumentException =
44 "java/lang/IllegalArgumentException";
49 static fields_t fields;
51 static status_t checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
52 if (env->ExceptionCheck()) {
53 ALOGE("An exception was thrown by callback '%s'.", methodName);
55 env->ExceptionClear();
61 // stolen from dalvik/vm/checkJni.cpp
62 static bool isValidUtf8(const char* bytes) {
63 while (*bytes != '\0') {
64 unsigned char utf8 = *(bytes++);
65 // Switch on the high four bits.
75 // Bit pattern 0xxx. No need for any extra bytes.
83 * Bit pattern 10xx or 1111, which are illegal start bytes.
84 * Note: 1111 is valid for normal UTF-8, but not the
85 * modified UTF-8 used here.
89 // Bit pattern 1110, so there are two additional bytes.
91 if ((utf8 & 0xc0) != 0x80) {
94 // Fall through to take care of the final byte.
97 // Bit pattern 110x, so there is one additional byte.
99 if ((utf8 & 0xc0) != 0x80) {
108 class MyMediaScannerClient : public MediaScannerClient
111 MyMediaScannerClient(JNIEnv *env, jobject client)
113 mClient(env->NewGlobalRef(client)),
114 mScanFileMethodID(0),
115 mHandleStringTagMethodID(0),
116 mSetMimeTypeMethodID(0)
118 ALOGV("MyMediaScannerClient constructor");
119 jclass mediaScannerClientInterface =
120 env->FindClass(kClassMediaScannerClient);
122 if (mediaScannerClientInterface == NULL) {
123 ALOGE("Class %s not found", kClassMediaScannerClient);
125 mScanFileMethodID = env->GetMethodID(
126 mediaScannerClientInterface,
128 "(Ljava/lang/String;JJZZ)V");
130 mHandleStringTagMethodID = env->GetMethodID(
131 mediaScannerClientInterface,
133 "(Ljava/lang/String;Ljava/lang/String;)V");
135 mSetMimeTypeMethodID = env->GetMethodID(
136 mediaScannerClientInterface,
138 "(Ljava/lang/String;)V");
142 virtual ~MyMediaScannerClient()
144 ALOGV("MyMediaScannerClient destructor");
145 mEnv->DeleteGlobalRef(mClient);
148 virtual status_t scanFile(const char* path, long long lastModified,
149 long long fileSize, bool isDirectory, bool noMedia)
151 ALOGV("scanFile: path(%s), time(%lld), size(%lld) and isDir(%d)",
152 path, lastModified, fileSize, isDirectory);
155 if ((pathStr = mEnv->NewStringUTF(path)) == NULL) {
156 mEnv->ExceptionClear();
160 mEnv->CallVoidMethod(mClient, mScanFileMethodID, pathStr, lastModified,
161 fileSize, isDirectory, noMedia);
163 mEnv->DeleteLocalRef(pathStr);
164 return checkAndClearExceptionFromCallback(mEnv, "scanFile");
167 virtual status_t handleStringTag(const char* name, const char* value)
169 ALOGV("handleStringTag: name(%s) and value(%s)", name, value);
170 jstring nameStr, valueStr;
171 if ((nameStr = mEnv->NewStringUTF(name)) == NULL) {
172 mEnv->ExceptionClear();
175 char *cleaned = NULL;
176 if (!isValidUtf8(value)) {
177 cleaned = strdup(value);
180 while ((ch = *chp)) {
188 valueStr = mEnv->NewStringUTF(value);
190 if (valueStr == NULL) {
191 mEnv->DeleteLocalRef(nameStr);
192 mEnv->ExceptionClear();
196 mEnv->CallVoidMethod(
197 mClient, mHandleStringTagMethodID, nameStr, valueStr);
199 mEnv->DeleteLocalRef(nameStr);
200 mEnv->DeleteLocalRef(valueStr);
201 return checkAndClearExceptionFromCallback(mEnv, "handleStringTag");
204 virtual status_t setMimeType(const char* mimeType)
206 ALOGV("setMimeType: %s", mimeType);
208 if ((mimeTypeStr = mEnv->NewStringUTF(mimeType)) == NULL) {
209 mEnv->ExceptionClear();
213 mEnv->CallVoidMethod(mClient, mSetMimeTypeMethodID, mimeTypeStr);
215 mEnv->DeleteLocalRef(mimeTypeStr);
216 return checkAndClearExceptionFromCallback(mEnv, "setMimeType");
222 jmethodID mScanFileMethodID;
223 jmethodID mHandleStringTagMethodID;
224 jmethodID mSetMimeTypeMethodID;
228 static MediaScanner *getNativeScanner_l(JNIEnv* env, jobject thiz)
230 return (MediaScanner *) env->GetLongField(thiz, fields.context);
233 static void setNativeScanner_l(JNIEnv* env, jobject thiz, MediaScanner *s)
235 env->SetLongField(thiz, fields.context, (jlong)s);
239 android_media_MediaScanner_processDirectory(
240 JNIEnv *env, jobject thiz, jstring path, jobject client)
242 ALOGV("processDirectory");
243 MediaScanner *mp = getNativeScanner_l(env, thiz);
245 jniThrowException(env, kRunTimeException, "No scanner available");
250 jniThrowException(env, kIllegalArgumentException, NULL);
254 const char *pathStr = env->GetStringUTFChars(path, NULL);
255 if (pathStr == NULL) { // Out of memory
259 MyMediaScannerClient myClient(env, client);
260 MediaScanResult result = mp->processDirectory(pathStr, myClient);
261 if (result == MEDIA_SCAN_RESULT_ERROR) {
262 ALOGE("An error occurred while scanning directory '%s'.", pathStr);
264 env->ReleaseStringUTFChars(path, pathStr);
268 android_media_MediaScanner_processFile(
269 JNIEnv *env, jobject thiz, jstring path,
270 jstring mimeType, jobject client)
272 ALOGV("processFile");
274 // Lock already hold by processDirectory
275 MediaScanner *mp = getNativeScanner_l(env, thiz);
277 jniThrowException(env, kRunTimeException, "No scanner available");
282 jniThrowException(env, kIllegalArgumentException, NULL);
286 const char *pathStr = env->GetStringUTFChars(path, NULL);
287 if (pathStr == NULL) { // Out of memory
291 const char *mimeTypeStr =
292 (mimeType ? env->GetStringUTFChars(mimeType, NULL) : NULL);
293 if (mimeType && mimeTypeStr == NULL) { // Out of memory
294 // ReleaseStringUTFChars can be called with an exception pending.
295 env->ReleaseStringUTFChars(path, pathStr);
299 MyMediaScannerClient myClient(env, client);
300 MediaScanResult result = mp->processFile(pathStr, mimeTypeStr, myClient);
301 if (result == MEDIA_SCAN_RESULT_ERROR) {
302 ALOGE("An error occurred while scanning file '%s'.", pathStr);
304 env->ReleaseStringUTFChars(path, pathStr);
306 env->ReleaseStringUTFChars(mimeType, mimeTypeStr);
311 android_media_MediaScanner_setLocale(
312 JNIEnv *env, jobject thiz, jstring locale)
315 MediaScanner *mp = getNativeScanner_l(env, thiz);
317 jniThrowException(env, kRunTimeException, "No scanner available");
321 if (locale == NULL) {
322 jniThrowException(env, kIllegalArgumentException, NULL);
325 const char *localeStr = env->GetStringUTFChars(locale, NULL);
326 if (localeStr == NULL) { // Out of memory
329 mp->setLocale(localeStr);
331 env->ReleaseStringUTFChars(locale, localeStr);
335 android_media_MediaScanner_extractAlbumArt(
336 JNIEnv *env, jobject thiz, jobject fileDescriptor)
338 ALOGV("extractAlbumArt");
339 MediaScanner *mp = getNativeScanner_l(env, thiz);
341 jniThrowException(env, kRunTimeException, "No scanner available");
345 if (fileDescriptor == NULL) {
346 jniThrowException(env, kIllegalArgumentException, NULL);
350 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
351 MediaAlbumArt* mediaAlbumArt = mp->extractAlbumArt(fd);
352 if (mediaAlbumArt == NULL) {
356 jbyteArray array = env->NewByteArray(mediaAlbumArt->size());
359 reinterpret_cast<const jbyte*>(mediaAlbumArt->data());
360 env->SetByteArrayRegion(array, 0, mediaAlbumArt->size(), data);
364 // if NewByteArray() returned NULL, an out-of-memory
365 // exception will have been raised. I just want to
366 // return null in that case.
367 env->ExceptionClear();
371 // This function gets a field ID, which in turn causes class initialization.
372 // It is called from a static block in MediaScanner, which won't run until the
373 // first time an instance of this class is used.
375 android_media_MediaScanner_native_init(JNIEnv *env)
377 ALOGV("native_init");
378 jclass clazz = env->FindClass(kClassMediaScanner);
383 fields.context = env->GetFieldID(clazz, "mNativeContext", "J");
384 if (fields.context == NULL) {
390 android_media_MediaScanner_native_setup(JNIEnv *env, jobject thiz)
392 ALOGV("native_setup");
393 MediaScanner *mp = new StagefrightMediaScanner;
396 jniThrowException(env, kRunTimeException, "Out of memory");
400 env->SetLongField(thiz, fields.context, (jlong)mp);
404 android_media_MediaScanner_native_finalize(JNIEnv *env, jobject thiz)
406 ALOGV("native_finalize");
407 MediaScanner *mp = getNativeScanner_l(env, thiz);
412 setNativeScanner_l(env, thiz, 0);
415 static const JNINativeMethod gMethods[] = {
418 "(Ljava/lang/String;Landroid/media/MediaScannerClient;)V",
419 (void *)android_media_MediaScanner_processDirectory
424 "(Ljava/lang/String;Ljava/lang/String;Landroid/media/MediaScannerClient;)V",
425 (void *)android_media_MediaScanner_processFile
430 "(Ljava/lang/String;)V",
431 (void *)android_media_MediaScanner_setLocale
436 "(Ljava/io/FileDescriptor;)[B",
437 (void *)android_media_MediaScanner_extractAlbumArt
443 (void *)android_media_MediaScanner_native_init
449 (void *)android_media_MediaScanner_native_setup
455 (void *)android_media_MediaScanner_native_finalize
459 // This function only registers the native methods, and is called from
460 // JNI_OnLoad in android_media_MediaPlayer.cpp
461 int register_android_media_MediaScanner(JNIEnv *env)
463 return AndroidRuntime::registerNativeMethods(env,
464 kClassMediaScanner, gMethods, NELEM(gMethods));