James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 1 | /* |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2 | ** |
| 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 Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "MediaScannerJNI" |
| 20 | #include <utils/Log.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #include <utils/threads.h> |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 22 | #include <media/mediascanner.h> |
| 23 | #include <media/stagefright/StagefrightMediaScanner.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
| 25 | #include "jni.h" |
| 26 | #include "JNIHelp.h" |
| 27 | #include "android_runtime/AndroidRuntime.h" |
| 28 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | using namespace android; |
| 30 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 32 | static const char* const kClassMediaScannerClient = |
| 33 | "android/media/MediaScannerClient"; |
| 34 | |
| 35 | static const char* const kClassMediaScanner = |
| 36 | "android/media/MediaScanner"; |
| 37 | |
| 38 | static const char* const kRunTimeException = |
| 39 | "java/lang/RuntimeException"; |
| 40 | |
| 41 | static const char* const kIllegalArgumentException = |
| 42 | "java/lang/IllegalArgumentException"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 44 | struct fields_t { |
| 45 | jfieldID context; |
| 46 | }; |
| 47 | static fields_t fields; |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 48 | |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame^] | 49 | static status_t checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) { |
| 50 | if (env->ExceptionCheck()) { |
| 51 | LOGE("An exception was thrown by callback '%s'.", methodName); |
| 52 | LOGE_EX(env); |
| 53 | env->ExceptionClear(); |
| 54 | return UNKNOWN_ERROR; |
| 55 | } |
| 56 | return OK; |
| 57 | } |
| 58 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | class MyMediaScannerClient : public MediaScannerClient |
| 60 | { |
| 61 | public: |
| 62 | MyMediaScannerClient(JNIEnv *env, jobject client) |
| 63 | : mEnv(env), |
| 64 | mClient(env->NewGlobalRef(client)), |
| 65 | mScanFileMethodID(0), |
| 66 | mHandleStringTagMethodID(0), |
| 67 | mSetMimeTypeMethodID(0) |
| 68 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 69 | LOGV("MyMediaScannerClient constructor"); |
| 70 | jclass mediaScannerClientInterface = |
| 71 | env->FindClass(kClassMediaScannerClient); |
| 72 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | if (mediaScannerClientInterface == NULL) { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 74 | LOGE("Class %s not found", kClassMediaScannerClient); |
| 75 | } else { |
| 76 | mScanFileMethodID = env->GetMethodID( |
| 77 | mediaScannerClientInterface, |
| 78 | "scanFile", |
Mike Lockwood | 997354e | 2011-04-24 11:15:09 -0700 | [diff] [blame] | 79 | "(Ljava/lang/String;JJZZ)V"); |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 80 | |
| 81 | mHandleStringTagMethodID = env->GetMethodID( |
| 82 | mediaScannerClientInterface, |
| 83 | "handleStringTag", |
| 84 | "(Ljava/lang/String;Ljava/lang/String;)V"); |
| 85 | |
| 86 | mSetMimeTypeMethodID = env->GetMethodID( |
| 87 | mediaScannerClientInterface, |
| 88 | "setMimeType", |
| 89 | "(Ljava/lang/String;)V"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | } |
| 91 | } |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 92 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | virtual ~MyMediaScannerClient() |
| 94 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 95 | LOGV("MyMediaScannerClient destructor"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | mEnv->DeleteGlobalRef(mClient); |
| 97 | } |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 98 | |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame^] | 99 | virtual status_t scanFile(const char* path, long long lastModified, |
Mike Lockwood | 997354e | 2011-04-24 11:15:09 -0700 | [diff] [blame] | 100 | long long fileSize, bool isDirectory, bool noMedia) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 102 | LOGV("scanFile: path(%s), time(%lld), size(%lld) and isDir(%d)", |
| 103 | path, lastModified, fileSize, isDirectory); |
| 104 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | jstring pathStr; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 106 | if ((pathStr = mEnv->NewStringUTF(path)) == NULL) { |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame^] | 107 | mEnv->ExceptionClear(); |
| 108 | return NO_MEMORY; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 109 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | |
Mike Lockwood | 076e05b | 2010-12-16 12:54:24 -0800 | [diff] [blame] | 111 | mEnv->CallVoidMethod(mClient, mScanFileMethodID, pathStr, lastModified, |
Mike Lockwood | 997354e | 2011-04-24 11:15:09 -0700 | [diff] [blame] | 112 | fileSize, isDirectory, noMedia); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | |
| 114 | mEnv->DeleteLocalRef(pathStr); |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame^] | 115 | return checkAndClearExceptionFromCallback(mEnv, "scanFile"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame^] | 118 | virtual status_t handleStringTag(const char* name, const char* value) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 120 | LOGV("handleStringTag: name(%s) and value(%s)", name, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | jstring nameStr, valueStr; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 122 | if ((nameStr = mEnv->NewStringUTF(name)) == NULL) { |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame^] | 123 | mEnv->ExceptionClear(); |
| 124 | return NO_MEMORY; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 125 | } |
| 126 | if ((valueStr = mEnv->NewStringUTF(value)) == NULL) { |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame^] | 127 | mEnv->DeleteLocalRef(nameStr); |
| 128 | mEnv->ExceptionClear(); |
| 129 | return NO_MEMORY; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 130 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 132 | mEnv->CallVoidMethod( |
| 133 | mClient, mHandleStringTagMethodID, nameStr, valueStr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | |
| 135 | mEnv->DeleteLocalRef(nameStr); |
| 136 | mEnv->DeleteLocalRef(valueStr); |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame^] | 137 | return checkAndClearExceptionFromCallback(mEnv, "handleStringTag"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame^] | 140 | virtual status_t setMimeType(const char* mimeType) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 142 | LOGV("setMimeType: %s", mimeType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | jstring mimeTypeStr; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 144 | if ((mimeTypeStr = mEnv->NewStringUTF(mimeType)) == NULL) { |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame^] | 145 | mEnv->ExceptionClear(); |
| 146 | return NO_MEMORY; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 147 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | |
| 149 | mEnv->CallVoidMethod(mClient, mSetMimeTypeMethodID, mimeTypeStr); |
| 150 | |
| 151 | mEnv->DeleteLocalRef(mimeTypeStr); |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame^] | 152 | return checkAndClearExceptionFromCallback(mEnv, "setMimeType"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | private: |
| 156 | JNIEnv *mEnv; |
| 157 | jobject mClient; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 158 | jmethodID mScanFileMethodID; |
| 159 | jmethodID mHandleStringTagMethodID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | jmethodID mSetMimeTypeMethodID; |
| 161 | }; |
| 162 | |
| 163 | |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 164 | static MediaScanner *getNativeScanner_l(JNIEnv* env, jobject thiz) |
| 165 | { |
| 166 | return (MediaScanner *) env->GetIntField(thiz, fields.context); |
| 167 | } |
| 168 | |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 169 | static void setNativeScanner_l(JNIEnv* env, jobject thiz, MediaScanner *s) |
| 170 | { |
| 171 | env->SetIntField(thiz, fields.context, (int)s); |
| 172 | } |
| 173 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | static void |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 175 | android_media_MediaScanner_processDirectory( |
| 176 | JNIEnv *env, jobject thiz, jstring path, jobject client) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 178 | LOGV("processDirectory"); |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 179 | MediaScanner *mp = getNativeScanner_l(env, thiz); |
| 180 | if (mp == NULL) { |
| 181 | jniThrowException(env, kRunTimeException, "No scanner available"); |
| 182 | return; |
| 183 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | |
| 185 | if (path == NULL) { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 186 | jniThrowException(env, kIllegalArgumentException, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | return; |
| 188 | } |
Mike Lockwood | c37255d | 2010-09-10 14:47:36 -0400 | [diff] [blame] | 189 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | const char *pathStr = env->GetStringUTFChars(path, NULL); |
| 191 | if (pathStr == NULL) { // Out of memory |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | return; |
| 193 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | |
| 195 | MyMediaScannerClient myClient(env, client); |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame^] | 196 | MediaScanResult result = mp->processDirectory(pathStr, myClient); |
| 197 | if (result == MEDIA_SCAN_RESULT_ERROR) { |
| 198 | LOGE("An error occurred while scanning directory '%s'.", pathStr); |
| 199 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | env->ReleaseStringUTFChars(path, pathStr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | static void |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 204 | android_media_MediaScanner_processFile( |
| 205 | JNIEnv *env, jobject thiz, jstring path, |
| 206 | jstring mimeType, jobject client) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 208 | LOGV("processFile"); |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 209 | |
| 210 | // Lock already hold by processDirectory |
| 211 | MediaScanner *mp = getNativeScanner_l(env, thiz); |
| 212 | if (mp == NULL) { |
| 213 | jniThrowException(env, kRunTimeException, "No scanner available"); |
| 214 | return; |
| 215 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | |
| 217 | if (path == NULL) { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 218 | jniThrowException(env, kIllegalArgumentException, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | return; |
| 220 | } |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 221 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | const char *pathStr = env->GetStringUTFChars(path, NULL); |
| 223 | if (pathStr == NULL) { // Out of memory |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | return; |
| 225 | } |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 226 | |
| 227 | const char *mimeTypeStr = |
| 228 | (mimeType ? env->GetStringUTFChars(mimeType, NULL) : NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | if (mimeType && mimeTypeStr == NULL) { // Out of memory |
James Dong | c371a02 | 2011-04-06 12:16:07 -0700 | [diff] [blame] | 230 | // ReleaseStringUTFChars can be called with an exception pending. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | env->ReleaseStringUTFChars(path, pathStr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | return; |
| 233 | } |
| 234 | |
| 235 | MyMediaScannerClient myClient(env, client); |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame^] | 236 | MediaScanResult result = mp->processFile(pathStr, mimeTypeStr, myClient); |
| 237 | if (result == MEDIA_SCAN_RESULT_ERROR) { |
| 238 | LOGE("An error occurred while scanning file '%s'.", pathStr); |
| 239 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | env->ReleaseStringUTFChars(path, pathStr); |
| 241 | if (mimeType) { |
| 242 | env->ReleaseStringUTFChars(mimeType, mimeTypeStr); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | static void |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 247 | android_media_MediaScanner_setLocale( |
| 248 | JNIEnv *env, jobject thiz, jstring locale) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 250 | LOGV("setLocale"); |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 251 | MediaScanner *mp = getNativeScanner_l(env, thiz); |
| 252 | if (mp == NULL) { |
| 253 | jniThrowException(env, kRunTimeException, "No scanner available"); |
| 254 | return; |
| 255 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | |
| 257 | if (locale == NULL) { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 258 | jniThrowException(env, kIllegalArgumentException, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | return; |
| 260 | } |
| 261 | const char *localeStr = env->GetStringUTFChars(locale, NULL); |
| 262 | if (localeStr == NULL) { // Out of memory |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | return; |
| 264 | } |
| 265 | mp->setLocale(localeStr); |
| 266 | |
| 267 | env->ReleaseStringUTFChars(locale, localeStr); |
| 268 | } |
| 269 | |
| 270 | static jbyteArray |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 271 | android_media_MediaScanner_extractAlbumArt( |
| 272 | JNIEnv *env, jobject thiz, jobject fileDescriptor) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 274 | LOGV("extractAlbumArt"); |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 275 | MediaScanner *mp = getNativeScanner_l(env, thiz); |
| 276 | if (mp == NULL) { |
| 277 | jniThrowException(env, kRunTimeException, "No scanner available"); |
| 278 | return NULL; |
| 279 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | |
| 281 | if (fileDescriptor == NULL) { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 282 | jniThrowException(env, kIllegalArgumentException, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | return NULL; |
| 284 | } |
| 285 | |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 286 | int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | char* data = mp->extractAlbumArt(fd); |
| 288 | if (!data) { |
| 289 | return NULL; |
| 290 | } |
| 291 | long len = *((long*)data); |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 292 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | jbyteArray array = env->NewByteArray(len); |
| 294 | if (array != NULL) { |
| 295 | jbyte* bytes = env->GetByteArrayElements(array, NULL); |
| 296 | memcpy(bytes, data + 4, len); |
| 297 | env->ReleaseByteArrayElements(array, bytes, 0); |
| 298 | } |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 299 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | done: |
| 301 | free(data); |
| 302 | // if NewByteArray() returned NULL, an out-of-memory |
| 303 | // exception will have been raised. I just want to |
| 304 | // return null in that case. |
| 305 | env->ExceptionClear(); |
| 306 | return array; |
| 307 | } |
| 308 | |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 309 | // This function gets a field ID, which in turn causes class initialization. |
| 310 | // It is called from a static block in MediaScanner, which won't run until the |
| 311 | // first time an instance of this class is used. |
| 312 | static void |
| 313 | android_media_MediaScanner_native_init(JNIEnv *env) |
| 314 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 315 | LOGV("native_init"); |
| 316 | jclass clazz = env->FindClass(kClassMediaScanner); |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 317 | if (clazz == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 318 | return; |
| 319 | } |
| 320 | |
| 321 | fields.context = env->GetFieldID(clazz, "mNativeContext", "I"); |
| 322 | if (fields.context == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 323 | return; |
| 324 | } |
| 325 | } |
| 326 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | static void |
| 328 | android_media_MediaScanner_native_setup(JNIEnv *env, jobject thiz) |
| 329 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 330 | LOGV("native_setup"); |
Andreas Huber | 8d65dd2 | 2010-06-23 16:40:57 -0700 | [diff] [blame] | 331 | MediaScanner *mp = new StagefrightMediaScanner; |
Andreas Huber | bfb9fb1 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 332 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | if (mp == NULL) { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 334 | jniThrowException(env, kRunTimeException, "Out of memory"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | return; |
| 336 | } |
| 337 | |
| 338 | env->SetIntField(thiz, fields.context, (int)mp); |
| 339 | } |
| 340 | |
| 341 | static void |
| 342 | android_media_MediaScanner_native_finalize(JNIEnv *env, jobject thiz) |
| 343 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 344 | LOGV("native_finalize"); |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 345 | MediaScanner *mp = getNativeScanner_l(env, thiz); |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 346 | if (mp == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | return; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 348 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | delete mp; |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 350 | setNativeScanner_l(env, thiz, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | } |
| 352 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | static JNINativeMethod gMethods[] = { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 354 | { |
| 355 | "processDirectory", |
| 356 | "(Ljava/lang/String;Landroid/media/MediaScannerClient;)V", |
| 357 | (void *)android_media_MediaScanner_processDirectory |
| 358 | }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 360 | { |
| 361 | "processFile", |
| 362 | "(Ljava/lang/String;Ljava/lang/String;Landroid/media/MediaScannerClient;)V", |
| 363 | (void *)android_media_MediaScanner_processFile |
| 364 | }, |
| 365 | |
| 366 | { |
| 367 | "setLocale", |
| 368 | "(Ljava/lang/String;)V", |
| 369 | (void *)android_media_MediaScanner_setLocale |
| 370 | }, |
| 371 | |
| 372 | { |
| 373 | "extractAlbumArt", |
| 374 | "(Ljava/io/FileDescriptor;)[B", |
| 375 | (void *)android_media_MediaScanner_extractAlbumArt |
| 376 | }, |
| 377 | |
| 378 | { |
| 379 | "native_init", |
| 380 | "()V", |
| 381 | (void *)android_media_MediaScanner_native_init |
| 382 | }, |
| 383 | |
| 384 | { |
| 385 | "native_setup", |
| 386 | "()V", |
| 387 | (void *)android_media_MediaScanner_native_setup |
| 388 | }, |
| 389 | |
| 390 | { |
| 391 | "native_finalize", |
| 392 | "()V", |
| 393 | (void *)android_media_MediaScanner_native_finalize |
| 394 | }, |
| 395 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 397 | // This function only registers the native methods, and is called from |
| 398 | // JNI_OnLoad in android_media_MediaPlayer.cpp |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | int register_android_media_MediaScanner(JNIEnv *env) |
| 400 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | return AndroidRuntime::registerNativeMethods(env, |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 402 | kClassMediaScanner, gMethods, NELEM(gMethods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | } |