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> |
Elliott Hughes | 9fa803b | 2014-06-05 10:38:30 -0700 | [diff] [blame] | 24 | #include <private/media/VideoFrame.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
| 26 | #include "jni.h" |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 27 | #include <nativehelper/JNIHelp.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | #include "android_runtime/AndroidRuntime.h" |
Ruben Brunk | 87eac99 | 2013-09-09 17:44:59 -0700 | [diff] [blame] | 29 | #include "android_runtime/Log.h" |
Ray Essick | 67004bc | 2018-11-01 13:57:11 -0700 | [diff] [blame] | 30 | #include <android-base/macros.h> // for FALLTHROUGH_INTENDED |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | using namespace android; |
| 33 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 35 | static const char* const kClassMediaScannerClient = |
| 36 | "android/media/MediaScannerClient"; |
| 37 | |
| 38 | static const char* const kClassMediaScanner = |
| 39 | "android/media/MediaScanner"; |
| 40 | |
| 41 | static const char* const kRunTimeException = |
| 42 | "java/lang/RuntimeException"; |
| 43 | |
| 44 | static const char* const kIllegalArgumentException = |
| 45 | "java/lang/IllegalArgumentException"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 47 | struct fields_t { |
| 48 | jfieldID context; |
| 49 | }; |
| 50 | static fields_t fields; |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 51 | |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 52 | static status_t checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) { |
| 53 | if (env->ExceptionCheck()) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 54 | ALOGE("An exception was thrown by callback '%s'.", methodName); |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 55 | LOGE_EX(env); |
| 56 | env->ExceptionClear(); |
| 57 | return UNKNOWN_ERROR; |
| 58 | } |
| 59 | return OK; |
| 60 | } |
| 61 | |
Marco Nelissen | a0a73ed | 2013-02-12 18:33:41 +0000 | [diff] [blame] | 62 | // stolen from dalvik/vm/checkJni.cpp |
| 63 | static bool isValidUtf8(const char* bytes) { |
| 64 | while (*bytes != '\0') { |
| 65 | unsigned char utf8 = *(bytes++); |
| 66 | // Switch on the high four bits. |
| 67 | switch (utf8 >> 4) { |
| 68 | case 0x00: |
| 69 | case 0x01: |
| 70 | case 0x02: |
| 71 | case 0x03: |
| 72 | case 0x04: |
| 73 | case 0x05: |
| 74 | case 0x06: |
| 75 | case 0x07: |
| 76 | // Bit pattern 0xxx. No need for any extra bytes. |
| 77 | break; |
| 78 | case 0x08: |
| 79 | case 0x09: |
| 80 | case 0x0a: |
| 81 | case 0x0b: |
| 82 | case 0x0f: |
| 83 | /* |
| 84 | * Bit pattern 10xx or 1111, which are illegal start bytes. |
| 85 | * Note: 1111 is valid for normal UTF-8, but not the |
| 86 | * modified UTF-8 used here. |
| 87 | */ |
| 88 | return false; |
| 89 | case 0x0e: |
| 90 | // Bit pattern 1110, so there are two additional bytes. |
| 91 | utf8 = *(bytes++); |
| 92 | if ((utf8 & 0xc0) != 0x80) { |
| 93 | return false; |
| 94 | } |
| 95 | // Fall through to take care of the final byte. |
Ray Essick | 67004bc | 2018-11-01 13:57:11 -0700 | [diff] [blame] | 96 | FALLTHROUGH_INTENDED; |
Marco Nelissen | a0a73ed | 2013-02-12 18:33:41 +0000 | [diff] [blame] | 97 | case 0x0c: |
| 98 | case 0x0d: |
| 99 | // Bit pattern 110x, so there is one additional byte. |
| 100 | utf8 = *(bytes++); |
| 101 | if ((utf8 & 0xc0) != 0x80) { |
| 102 | return false; |
| 103 | } |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | return true; |
| 108 | } |
| 109 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | class MyMediaScannerClient : public MediaScannerClient |
| 111 | { |
| 112 | public: |
| 113 | MyMediaScannerClient(JNIEnv *env, jobject client) |
| 114 | : mEnv(env), |
| 115 | mClient(env->NewGlobalRef(client)), |
| 116 | mScanFileMethodID(0), |
| 117 | mHandleStringTagMethodID(0), |
| 118 | mSetMimeTypeMethodID(0) |
| 119 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 120 | ALOGV("MyMediaScannerClient constructor"); |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 121 | jclass mediaScannerClientInterface = |
| 122 | env->FindClass(kClassMediaScannerClient); |
| 123 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | if (mediaScannerClientInterface == NULL) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 125 | ALOGE("Class %s not found", kClassMediaScannerClient); |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 126 | } else { |
| 127 | mScanFileMethodID = env->GetMethodID( |
| 128 | mediaScannerClientInterface, |
| 129 | "scanFile", |
Mike Lockwood | 997354e | 2011-04-24 11:15:09 -0700 | [diff] [blame] | 130 | "(Ljava/lang/String;JJZZ)V"); |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 131 | |
| 132 | mHandleStringTagMethodID = env->GetMethodID( |
| 133 | mediaScannerClientInterface, |
| 134 | "handleStringTag", |
| 135 | "(Ljava/lang/String;Ljava/lang/String;)V"); |
| 136 | |
| 137 | mSetMimeTypeMethodID = env->GetMethodID( |
| 138 | mediaScannerClientInterface, |
| 139 | "setMimeType", |
| 140 | "(Ljava/lang/String;)V"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | } |
| 142 | } |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 143 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | virtual ~MyMediaScannerClient() |
| 145 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 146 | ALOGV("MyMediaScannerClient destructor"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | mEnv->DeleteGlobalRef(mClient); |
| 148 | } |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 149 | |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 150 | virtual status_t scanFile(const char* path, long long lastModified, |
Mike Lockwood | 997354e | 2011-04-24 11:15:09 -0700 | [diff] [blame] | 151 | long long fileSize, bool isDirectory, bool noMedia) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 153 | ALOGV("scanFile: path(%s), time(%lld), size(%lld) and isDir(%d)", |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 154 | path, lastModified, fileSize, isDirectory); |
| 155 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | jstring pathStr; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 157 | if ((pathStr = mEnv->NewStringUTF(path)) == NULL) { |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 158 | mEnv->ExceptionClear(); |
| 159 | return NO_MEMORY; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 160 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | |
Mike Lockwood | 076e05b | 2010-12-16 12:54:24 -0800 | [diff] [blame] | 162 | mEnv->CallVoidMethod(mClient, mScanFileMethodID, pathStr, lastModified, |
Mike Lockwood | 997354e | 2011-04-24 11:15:09 -0700 | [diff] [blame] | 163 | fileSize, isDirectory, noMedia); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | |
| 165 | mEnv->DeleteLocalRef(pathStr); |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 166 | return checkAndClearExceptionFromCallback(mEnv, "scanFile"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 169 | 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] | 170 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 171 | ALOGV("handleStringTag: name(%s) and value(%s)", name, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | jstring nameStr, valueStr; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 173 | if ((nameStr = mEnv->NewStringUTF(name)) == NULL) { |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 174 | mEnv->ExceptionClear(); |
| 175 | return NO_MEMORY; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 176 | } |
Marco Nelissen | f51f1bd | 2011-11-02 10:49:50 -0700 | [diff] [blame] | 177 | char *cleaned = NULL; |
Marco Nelissen | a0a73ed | 2013-02-12 18:33:41 +0000 | [diff] [blame] | 178 | if (!isValidUtf8(value)) { |
Marco Nelissen | f51f1bd | 2011-11-02 10:49:50 -0700 | [diff] [blame] | 179 | cleaned = strdup(value); |
| 180 | char *chp = cleaned; |
| 181 | char ch; |
| 182 | while ((ch = *chp)) { |
| 183 | if (ch & 0x80) { |
| 184 | *chp = '?'; |
| 185 | } |
| 186 | chp++; |
| 187 | } |
| 188 | value = cleaned; |
| 189 | } |
| 190 | valueStr = mEnv->NewStringUTF(value); |
| 191 | free(cleaned); |
| 192 | if (valueStr == NULL) { |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 193 | mEnv->DeleteLocalRef(nameStr); |
| 194 | mEnv->ExceptionClear(); |
| 195 | return NO_MEMORY; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 196 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 198 | mEnv->CallVoidMethod( |
| 199 | mClient, mHandleStringTagMethodID, nameStr, valueStr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | |
| 201 | mEnv->DeleteLocalRef(nameStr); |
| 202 | mEnv->DeleteLocalRef(valueStr); |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 203 | return checkAndClearExceptionFromCallback(mEnv, "handleStringTag"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 206 | virtual status_t setMimeType(const char* mimeType) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 208 | ALOGV("setMimeType: %s", mimeType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | jstring mimeTypeStr; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 210 | if ((mimeTypeStr = mEnv->NewStringUTF(mimeType)) == NULL) { |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 211 | mEnv->ExceptionClear(); |
| 212 | return NO_MEMORY; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 213 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | |
| 215 | mEnv->CallVoidMethod(mClient, mSetMimeTypeMethodID, mimeTypeStr); |
| 216 | |
| 217 | mEnv->DeleteLocalRef(mimeTypeStr); |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 218 | return checkAndClearExceptionFromCallback(mEnv, "setMimeType"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | private: |
| 222 | JNIEnv *mEnv; |
| 223 | jobject mClient; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 224 | jmethodID mScanFileMethodID; |
| 225 | jmethodID mHandleStringTagMethodID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | jmethodID mSetMimeTypeMethodID; |
| 227 | }; |
| 228 | |
| 229 | |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 230 | static MediaScanner *getNativeScanner_l(JNIEnv* env, jobject thiz) |
| 231 | { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 232 | return (MediaScanner *) env->GetLongField(thiz, fields.context); |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 233 | } |
| 234 | |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 235 | static void setNativeScanner_l(JNIEnv* env, jobject thiz, MediaScanner *s) |
| 236 | { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 237 | env->SetLongField(thiz, fields.context, (jlong)s); |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 238 | } |
| 239 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | static void |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 241 | android_media_MediaScanner_processDirectory( |
| 242 | JNIEnv *env, jobject thiz, jstring path, jobject client) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 244 | ALOGV("processDirectory"); |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 245 | MediaScanner *mp = getNativeScanner_l(env, thiz); |
| 246 | if (mp == NULL) { |
| 247 | jniThrowException(env, kRunTimeException, "No scanner available"); |
| 248 | return; |
| 249 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | |
| 251 | if (path == NULL) { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 252 | jniThrowException(env, kIllegalArgumentException, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | return; |
| 254 | } |
Mike Lockwood | c37255d | 2010-09-10 14:47:36 -0400 | [diff] [blame] | 255 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | const char *pathStr = env->GetStringUTFChars(path, NULL); |
| 257 | if (pathStr == NULL) { // Out of memory |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | return; |
| 259 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | |
| 261 | MyMediaScannerClient myClient(env, client); |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 262 | MediaScanResult result = mp->processDirectory(pathStr, myClient); |
| 263 | if (result == MEDIA_SCAN_RESULT_ERROR) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 264 | ALOGE("An error occurred while scanning directory '%s'.", pathStr); |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 265 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | env->ReleaseStringUTFChars(path, pathStr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Marco Nelissen | bab7590 | 2018-04-25 12:24:11 -0700 | [diff] [blame] | 269 | static jboolean |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 270 | android_media_MediaScanner_processFile( |
| 271 | JNIEnv *env, jobject thiz, jstring path, |
| 272 | jstring mimeType, jobject client) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 274 | ALOGV("processFile"); |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 275 | |
| 276 | // Lock already hold by processDirectory |
| 277 | MediaScanner *mp = getNativeScanner_l(env, thiz); |
| 278 | if (mp == NULL) { |
| 279 | jniThrowException(env, kRunTimeException, "No scanner available"); |
Marco Nelissen | bab7590 | 2018-04-25 12:24:11 -0700 | [diff] [blame] | 280 | return false; |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 281 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | |
| 283 | if (path == NULL) { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 284 | jniThrowException(env, kIllegalArgumentException, NULL); |
Marco Nelissen | bab7590 | 2018-04-25 12:24:11 -0700 | [diff] [blame] | 285 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | } |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 287 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | const char *pathStr = env->GetStringUTFChars(path, NULL); |
| 289 | if (pathStr == NULL) { // Out of memory |
Marco Nelissen | bab7590 | 2018-04-25 12:24:11 -0700 | [diff] [blame] | 290 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | } |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 292 | |
| 293 | const char *mimeTypeStr = |
| 294 | (mimeType ? env->GetStringUTFChars(mimeType, NULL) : NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | if (mimeType && mimeTypeStr == NULL) { // Out of memory |
James Dong | c371a02 | 2011-04-06 12:16:07 -0700 | [diff] [blame] | 296 | // ReleaseStringUTFChars can be called with an exception pending. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | env->ReleaseStringUTFChars(path, pathStr); |
Marco Nelissen | bab7590 | 2018-04-25 12:24:11 -0700 | [diff] [blame] | 298 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | MyMediaScannerClient myClient(env, client); |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 302 | MediaScanResult result = mp->processFile(pathStr, mimeTypeStr, myClient); |
| 303 | if (result == MEDIA_SCAN_RESULT_ERROR) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 304 | ALOGE("An error occurred while scanning file '%s'.", pathStr); |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 305 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | env->ReleaseStringUTFChars(path, pathStr); |
| 307 | if (mimeType) { |
| 308 | env->ReleaseStringUTFChars(mimeType, mimeTypeStr); |
| 309 | } |
Marco Nelissen | bab7590 | 2018-04-25 12:24:11 -0700 | [diff] [blame] | 310 | return result != MEDIA_SCAN_RESULT_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static void |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 314 | android_media_MediaScanner_setLocale( |
| 315 | JNIEnv *env, jobject thiz, jstring locale) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 317 | ALOGV("setLocale"); |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 318 | MediaScanner *mp = getNativeScanner_l(env, thiz); |
| 319 | if (mp == NULL) { |
| 320 | jniThrowException(env, kRunTimeException, "No scanner available"); |
| 321 | return; |
| 322 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | |
| 324 | if (locale == NULL) { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 325 | jniThrowException(env, kIllegalArgumentException, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | return; |
| 327 | } |
| 328 | const char *localeStr = env->GetStringUTFChars(locale, NULL); |
| 329 | if (localeStr == NULL) { // Out of memory |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | return; |
| 331 | } |
| 332 | mp->setLocale(localeStr); |
| 333 | |
| 334 | env->ReleaseStringUTFChars(locale, localeStr); |
| 335 | } |
| 336 | |
| 337 | static jbyteArray |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 338 | android_media_MediaScanner_extractAlbumArt( |
| 339 | JNIEnv *env, jobject thiz, jobject fileDescriptor) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 341 | ALOGV("extractAlbumArt"); |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 342 | MediaScanner *mp = getNativeScanner_l(env, thiz); |
| 343 | if (mp == NULL) { |
| 344 | jniThrowException(env, kRunTimeException, "No scanner available"); |
| 345 | return NULL; |
| 346 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | |
| 348 | if (fileDescriptor == NULL) { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 349 | jniThrowException(env, kIllegalArgumentException, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 | return NULL; |
| 351 | } |
| 352 | |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 353 | int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
Elliott Hughes | 95d3f86 | 2014-06-10 16:53:31 -0700 | [diff] [blame] | 354 | MediaAlbumArt* mediaAlbumArt = mp->extractAlbumArt(fd); |
Elliott Hughes | e0f2fa3 | 2014-06-09 09:22:58 -0700 | [diff] [blame] | 355 | if (mediaAlbumArt == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | return NULL; |
| 357 | } |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 358 | |
Elliott Hughes | 95d3f86 | 2014-06-10 16:53:31 -0700 | [diff] [blame] | 359 | jbyteArray array = env->NewByteArray(mediaAlbumArt->size()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | if (array != NULL) { |
Elliott Hughes | 95d3f86 | 2014-06-10 16:53:31 -0700 | [diff] [blame] | 361 | const jbyte* data = |
| 362 | reinterpret_cast<const jbyte*>(mediaAlbumArt->data()); |
| 363 | env->SetByteArrayRegion(array, 0, mediaAlbumArt->size(), data); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | } |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 365 | |
Elliott Hughes | 9fa803b | 2014-06-05 10:38:30 -0700 | [diff] [blame] | 366 | free(mediaAlbumArt); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | // if NewByteArray() returned NULL, an out-of-memory |
| 368 | // exception will have been raised. I just want to |
| 369 | // return null in that case. |
| 370 | env->ExceptionClear(); |
| 371 | return array; |
| 372 | } |
| 373 | |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 374 | // This function gets a field ID, which in turn causes class initialization. |
| 375 | // It is called from a static block in MediaScanner, which won't run until the |
| 376 | // first time an instance of this class is used. |
| 377 | static void |
| 378 | android_media_MediaScanner_native_init(JNIEnv *env) |
| 379 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 380 | ALOGV("native_init"); |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 381 | jclass clazz = env->FindClass(kClassMediaScanner); |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 382 | if (clazz == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 383 | return; |
| 384 | } |
| 385 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 386 | fields.context = env->GetFieldID(clazz, "mNativeContext", "J"); |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 387 | if (fields.context == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 388 | return; |
| 389 | } |
| 390 | } |
| 391 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | static void |
| 393 | android_media_MediaScanner_native_setup(JNIEnv *env, jobject thiz) |
| 394 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 395 | ALOGV("native_setup"); |
Andreas Huber | 8d65dd2 | 2010-06-23 16:40:57 -0700 | [diff] [blame] | 396 | MediaScanner *mp = new StagefrightMediaScanner; |
Andreas Huber | bfb9fb1 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 397 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | if (mp == NULL) { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 399 | jniThrowException(env, kRunTimeException, "Out of memory"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | return; |
| 401 | } |
| 402 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 403 | env->SetLongField(thiz, fields.context, (jlong)mp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | static void |
| 407 | android_media_MediaScanner_native_finalize(JNIEnv *env, jobject thiz) |
| 408 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 409 | ALOGV("native_finalize"); |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 410 | MediaScanner *mp = getNativeScanner_l(env, thiz); |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 411 | if (mp == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | return; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 413 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | delete mp; |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 415 | setNativeScanner_l(env, thiz, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 418 | static const JNINativeMethod gMethods[] = { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 419 | { |
| 420 | "processDirectory", |
| 421 | "(Ljava/lang/String;Landroid/media/MediaScannerClient;)V", |
| 422 | (void *)android_media_MediaScanner_processDirectory |
| 423 | }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 425 | { |
| 426 | "processFile", |
Marco Nelissen | bab7590 | 2018-04-25 12:24:11 -0700 | [diff] [blame] | 427 | "(Ljava/lang/String;Ljava/lang/String;Landroid/media/MediaScannerClient;)Z", |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 428 | (void *)android_media_MediaScanner_processFile |
| 429 | }, |
| 430 | |
| 431 | { |
| 432 | "setLocale", |
| 433 | "(Ljava/lang/String;)V", |
| 434 | (void *)android_media_MediaScanner_setLocale |
| 435 | }, |
| 436 | |
| 437 | { |
| 438 | "extractAlbumArt", |
| 439 | "(Ljava/io/FileDescriptor;)[B", |
| 440 | (void *)android_media_MediaScanner_extractAlbumArt |
| 441 | }, |
| 442 | |
| 443 | { |
| 444 | "native_init", |
| 445 | "()V", |
| 446 | (void *)android_media_MediaScanner_native_init |
| 447 | }, |
| 448 | |
| 449 | { |
| 450 | "native_setup", |
| 451 | "()V", |
| 452 | (void *)android_media_MediaScanner_native_setup |
| 453 | }, |
| 454 | |
| 455 | { |
| 456 | "native_finalize", |
| 457 | "()V", |
| 458 | (void *)android_media_MediaScanner_native_finalize |
| 459 | }, |
| 460 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 462 | // This function only registers the native methods, and is called from |
| 463 | // JNI_OnLoad in android_media_MediaPlayer.cpp |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | int register_android_media_MediaScanner(JNIEnv *env) |
| 465 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | return AndroidRuntime::registerNativeMethods(env, |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 467 | kClassMediaScanner, gMethods, NELEM(gMethods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 468 | } |