The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2008, 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 | |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "MediaMetadataRetrieverJNI" |
| 20 | |
| 21 | #include <assert.h> |
| 22 | #include <utils/Log.h> |
| 23 | #include <utils/threads.h> |
| 24 | #include <core/SkBitmap.h> |
Andreas Huber | d2506a5 | 2014-01-29 10:32:46 -0800 | [diff] [blame] | 25 | #include <media/IMediaHTTPService.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | #include <media/mediametadataretriever.h> |
| 27 | #include <private/media/VideoFrame.h> |
| 28 | |
| 29 | #include "jni.h" |
| 30 | #include "JNIHelp.h" |
| 31 | #include "android_runtime/AndroidRuntime.h" |
James Dong | 79f407c | 2011-05-05 12:50:04 -0700 | [diff] [blame] | 32 | #include "android_media_Utils.h" |
Andreas Huber | d2506a5 | 2014-01-29 10:32:46 -0800 | [diff] [blame] | 33 | #include "android_util_Binder.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | |
| 35 | |
| 36 | using namespace android; |
| 37 | |
| 38 | struct fields_t { |
| 39 | jfieldID context; |
James Dong | c371a02 | 2011-04-06 12:16:07 -0700 | [diff] [blame] | 40 | jclass bitmapClazz; // Must be a global ref |
James Dong | 0e4b535 | 2010-12-19 13:05:33 -0800 | [diff] [blame] | 41 | jfieldID nativeBitmap; |
James Dong | 53ebc72 | 2010-11-08 16:04:27 -0800 | [diff] [blame] | 42 | jmethodID createBitmapMethod; |
James Dong | 9f2cde3 | 2011-03-18 17:55:06 -0700 | [diff] [blame] | 43 | jmethodID createScaledBitmapMethod; |
James Dong | c371a02 | 2011-04-06 12:16:07 -0700 | [diff] [blame] | 44 | jclass configClazz; // Must be a global ref |
James Dong | 0e4b535 | 2010-12-19 13:05:33 -0800 | [diff] [blame] | 45 | jmethodID createConfigMethod; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | static fields_t fields; |
| 49 | static Mutex sLock; |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 50 | static const char* const kClassPathName = "android/media/MediaMetadataRetriever"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | |
| 52 | static void process_media_retriever_call(JNIEnv *env, status_t opStatus, const char* exception, const char *message) |
| 53 | { |
| 54 | if (opStatus == (status_t) INVALID_OPERATION) { |
| 55 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 56 | } else if (opStatus != (status_t) OK) { |
| 57 | if (strlen(message) > 230) { |
| 58 | // If the message is too long, don't bother displaying the status code. |
| 59 | jniThrowException( env, exception, message); |
| 60 | } else { |
| 61 | char msg[256]; |
| 62 | // Append the status code to the message. |
| 63 | sprintf(msg, "%s: status = 0x%X", message, opStatus); |
| 64 | jniThrowException( env, exception, msg); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | static MediaMetadataRetriever* getRetriever(JNIEnv* env, jobject thiz) |
| 70 | { |
| 71 | // No lock is needed, since it is called internally by other methods that are protected |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 72 | MediaMetadataRetriever* retriever = (MediaMetadataRetriever*) env->GetLongField(thiz, fields.context); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | return retriever; |
| 74 | } |
| 75 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 76 | static void setRetriever(JNIEnv* env, jobject thiz, MediaMetadataRetriever* retriever) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | { |
| 78 | // No lock is needed, since it is called internally by other methods that are protected |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 79 | MediaMetadataRetriever *old = (MediaMetadataRetriever*) env->GetLongField(thiz, fields.context); |
| 80 | env->SetLongField(thiz, fields.context, (jlong) retriever); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Andreas Huber | 5b7ced6 | 2011-03-21 10:25:44 -0700 | [diff] [blame] | 83 | static void |
| 84 | android_media_MediaMetadataRetriever_setDataSourceAndHeaders( |
Andreas Huber | d2506a5 | 2014-01-29 10:32:46 -0800 | [diff] [blame] | 85 | JNIEnv *env, jobject thiz, jobject httpServiceBinderObj, jstring path, |
James Dong | 17524dc | 2011-05-04 13:41:58 -0700 | [diff] [blame] | 86 | jobjectArray keys, jobjectArray values) { |
| 87 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 88 | ALOGV("setDataSource"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | MediaMetadataRetriever* retriever = getRetriever(env, thiz); |
| 90 | if (retriever == 0) { |
Andreas Huber | 5b7ced6 | 2011-03-21 10:25:44 -0700 | [diff] [blame] | 91 | jniThrowException( |
| 92 | env, |
| 93 | "java/lang/IllegalStateException", "No retriever available"); |
| 94 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | return; |
| 96 | } |
| 97 | |
Andreas Huber | 5b7ced6 | 2011-03-21 10:25:44 -0700 | [diff] [blame] | 98 | if (!path) { |
| 99 | jniThrowException( |
| 100 | env, "java/lang/IllegalArgumentException", "Null pointer"); |
| 101 | |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | const char *tmp = env->GetStringUTFChars(path, NULL); |
Andreas Huber | 5bb357f | 2011-03-21 11:55:01 -0700 | [diff] [blame] | 106 | if (!tmp) { // OutOfMemoryError exception already thrown |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | return; |
| 108 | } |
| 109 | |
Andreas Huber | 5bb357f | 2011-03-21 11:55:01 -0700 | [diff] [blame] | 110 | String8 pathStr(tmp); |
Andreas Huber | 5b7ced6 | 2011-03-21 10:25:44 -0700 | [diff] [blame] | 111 | env->ReleaseStringUTFChars(path, tmp); |
| 112 | tmp = NULL; |
| 113 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | // Don't let somebody trick us in to reading some random block of memory |
Andreas Huber | 5b7ced6 | 2011-03-21 10:25:44 -0700 | [diff] [blame] | 115 | if (strncmp("mem://", pathStr.string(), 6) == 0) { |
| 116 | jniThrowException( |
| 117 | env, "java/lang/IllegalArgumentException", "Invalid pathname"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | return; |
| 119 | } |
| 120 | |
Andreas Huber | 5b7ced6 | 2011-03-21 10:25:44 -0700 | [diff] [blame] | 121 | // We build a similar KeyedVector out of it. |
| 122 | KeyedVector<String8, String8> headersVector; |
James Dong | 79f407c | 2011-05-05 12:50:04 -0700 | [diff] [blame] | 123 | if (!ConvertKeyValueArraysToKeyedVector( |
| 124 | env, keys, values, &headersVector)) { |
| 125 | return; |
Andreas Huber | 5b7ced6 | 2011-03-21 10:25:44 -0700 | [diff] [blame] | 126 | } |
Andreas Huber | d2506a5 | 2014-01-29 10:32:46 -0800 | [diff] [blame] | 127 | |
| 128 | sp<IMediaHTTPService> httpService; |
| 129 | if (httpServiceBinderObj != NULL) { |
| 130 | sp<IBinder> binder = ibinderForJavaObject(env, httpServiceBinderObj); |
| 131 | httpService = interface_cast<IMediaHTTPService>(binder); |
| 132 | } |
| 133 | |
Andreas Huber | 5b7ced6 | 2011-03-21 10:25:44 -0700 | [diff] [blame] | 134 | process_media_retriever_call( |
| 135 | env, |
| 136 | retriever->setDataSource( |
Andreas Huber | d2506a5 | 2014-01-29 10:32:46 -0800 | [diff] [blame] | 137 | httpService, |
| 138 | pathStr.string(), |
| 139 | headersVector.size() > 0 ? &headersVector : NULL), |
Andreas Huber | 5b7ced6 | 2011-03-21 10:25:44 -0700 | [diff] [blame] | 140 | |
| 141 | "java/lang/RuntimeException", |
| 142 | "setDataSource failed"); |
| 143 | } |
| 144 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | static void android_media_MediaMetadataRetriever_setDataSourceFD(JNIEnv *env, jobject thiz, jobject fileDescriptor, jlong offset, jlong length) |
| 146 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 147 | ALOGV("setDataSource"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | MediaMetadataRetriever* retriever = getRetriever(env, thiz); |
| 149 | if (retriever == 0) { |
| 150 | jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); |
| 151 | return; |
| 152 | } |
| 153 | if (!fileDescriptor) { |
| 154 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 155 | return; |
| 156 | } |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 157 | int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | if (offset < 0 || length < 0 || fd < 0) { |
| 159 | if (offset < 0) { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 160 | ALOGE("negative offset (%lld)", (long long)offset); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | } |
| 162 | if (length < 0) { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 163 | ALOGE("negative length (%lld)", (long long)length); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | } |
| 165 | if (fd < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 166 | ALOGE("invalid file descriptor"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | } |
| 168 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 169 | return; |
| 170 | } |
| 171 | process_media_retriever_call(env, retriever->setDataSource(fd, offset, length), "java/lang/RuntimeException", "setDataSource failed"); |
| 172 | } |
| 173 | |
Carl Shapiro | ae12a50 | 2011-01-20 23:13:09 -0800 | [diff] [blame] | 174 | template<typename T> |
| 175 | static void rotate0(T* dst, const T* src, size_t width, size_t height) |
| 176 | { |
| 177 | memcpy(dst, src, width * height * sizeof(T)); |
| 178 | } |
| 179 | |
| 180 | template<typename T> |
| 181 | static void rotate90(T* dst, const T* src, size_t width, size_t height) |
| 182 | { |
| 183 | for (size_t i = 0; i < height; ++i) { |
| 184 | for (size_t j = 0; j < width; ++j) { |
| 185 | dst[j * height + height - 1 - i] = src[i * width + j]; |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | template<typename T> |
| 191 | static void rotate180(T* dst, const T* src, size_t width, size_t height) |
| 192 | { |
| 193 | for (size_t i = 0; i < height; ++i) { |
| 194 | for (size_t j = 0; j < width; ++j) { |
| 195 | dst[(height - 1 - i) * width + width - 1 - j] = src[i * width + j]; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | template<typename T> |
| 201 | static void rotate270(T* dst, const T* src, size_t width, size_t height) |
| 202 | { |
| 203 | for (size_t i = 0; i < height; ++i) { |
| 204 | for (size_t j = 0; j < width; ++j) { |
| 205 | dst[(width - 1 - j) * height + i] = src[i * width + j]; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | template<typename T> |
| 211 | static void rotate(T *dst, const T *src, size_t width, size_t height, int angle) |
| 212 | { |
| 213 | switch (angle) { |
| 214 | case 0: |
| 215 | rotate0(dst, src, width, height); |
| 216 | break; |
| 217 | case 90: |
| 218 | rotate90(dst, src, width, height); |
| 219 | break; |
| 220 | case 180: |
| 221 | rotate180(dst, src, width, height); |
| 222 | break; |
| 223 | case 270: |
| 224 | rotate270(dst, src, width, height); |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | |
James Dong | faf09ba | 2010-12-02 17:42:08 -0800 | [diff] [blame] | 229 | static jobject android_media_MediaMetadataRetriever_getFrameAtTime(JNIEnv *env, jobject thiz, jlong timeUs, jint option) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 231 | ALOGV("getFrameAtTime: %lld us option: %d", timeUs, option); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | MediaMetadataRetriever* retriever = getRetriever(env, thiz); |
| 233 | if (retriever == 0) { |
| 234 | jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); |
| 235 | return NULL; |
| 236 | } |
| 237 | |
| 238 | // Call native method to retrieve a video frame |
| 239 | VideoFrame *videoFrame = NULL; |
James Dong | faf09ba | 2010-12-02 17:42:08 -0800 | [diff] [blame] | 240 | sp<IMemory> frameMemory = retriever->getFrameAtTime(timeUs, option); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | if (frameMemory != 0) { // cast the shared structure to a VideoFrame object |
| 242 | videoFrame = static_cast<VideoFrame *>(frameMemory->pointer()); |
| 243 | } |
| 244 | if (videoFrame == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 245 | ALOGE("getFrameAtTime: videoFrame is a NULL pointer"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | return NULL; |
| 247 | } |
| 248 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 249 | ALOGV("Dimension = %dx%d and bytes = %d", |
James Dong | 0e4b535 | 2010-12-19 13:05:33 -0800 | [diff] [blame] | 250 | videoFrame->mDisplayWidth, |
| 251 | videoFrame->mDisplayHeight, |
| 252 | videoFrame->mSize); |
| 253 | |
James Dong | 0e4b535 | 2010-12-19 13:05:33 -0800 | [diff] [blame] | 254 | jobject config = env->CallStaticObjectMethod( |
| 255 | fields.configClazz, |
| 256 | fields.createConfigMethod, |
| 257 | SkBitmap::kRGB_565_Config); |
| 258 | |
Carl Shapiro | ae12a50 | 2011-01-20 23:13:09 -0800 | [diff] [blame] | 259 | size_t width, height; |
James Dong | 9f2cde3 | 2011-03-18 17:55:06 -0700 | [diff] [blame] | 260 | bool swapWidthAndHeight = false; |
Carl Shapiro | ae12a50 | 2011-01-20 23:13:09 -0800 | [diff] [blame] | 261 | if (videoFrame->mRotationAngle == 90 || videoFrame->mRotationAngle == 270) { |
James Dong | 9f2cde3 | 2011-03-18 17:55:06 -0700 | [diff] [blame] | 262 | width = videoFrame->mHeight; |
| 263 | height = videoFrame->mWidth; |
| 264 | swapWidthAndHeight = true; |
Carl Shapiro | ae12a50 | 2011-01-20 23:13:09 -0800 | [diff] [blame] | 265 | } else { |
James Dong | 9f2cde3 | 2011-03-18 17:55:06 -0700 | [diff] [blame] | 266 | width = videoFrame->mWidth; |
| 267 | height = videoFrame->mHeight; |
Carl Shapiro | ae12a50 | 2011-01-20 23:13:09 -0800 | [diff] [blame] | 268 | } |
| 269 | |
James Dong | 0e4b535 | 2010-12-19 13:05:33 -0800 | [diff] [blame] | 270 | jobject jBitmap = env->CallStaticObjectMethod( |
| 271 | fields.bitmapClazz, |
| 272 | fields.createBitmapMethod, |
Carl Shapiro | ae12a50 | 2011-01-20 23:13:09 -0800 | [diff] [blame] | 273 | width, |
| 274 | height, |
James Dong | 0e4b535 | 2010-12-19 13:05:33 -0800 | [diff] [blame] | 275 | config); |
Carl Shapiro | ae12a50 | 2011-01-20 23:13:09 -0800 | [diff] [blame] | 276 | |
James Dong | 0e4b535 | 2010-12-19 13:05:33 -0800 | [diff] [blame] | 277 | SkBitmap *bitmap = |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 278 | (SkBitmap *) env->GetLongField(jBitmap, fields.nativeBitmap); |
James Dong | 0e4b535 | 2010-12-19 13:05:33 -0800 | [diff] [blame] | 279 | |
| 280 | bitmap->lockPixels(); |
Carl Shapiro | ae12a50 | 2011-01-20 23:13:09 -0800 | [diff] [blame] | 281 | rotate((uint16_t*)bitmap->getPixels(), |
| 282 | (uint16_t*)((char*)videoFrame + sizeof(VideoFrame)), |
James Dong | 9f2cde3 | 2011-03-18 17:55:06 -0700 | [diff] [blame] | 283 | videoFrame->mWidth, |
| 284 | videoFrame->mHeight, |
Carl Shapiro | ae12a50 | 2011-01-20 23:13:09 -0800 | [diff] [blame] | 285 | videoFrame->mRotationAngle); |
James Dong | 0e4b535 | 2010-12-19 13:05:33 -0800 | [diff] [blame] | 286 | bitmap->unlockPixels(); |
| 287 | |
James Dong | 9f2cde3 | 2011-03-18 17:55:06 -0700 | [diff] [blame] | 288 | if (videoFrame->mDisplayWidth != videoFrame->mWidth || |
| 289 | videoFrame->mDisplayHeight != videoFrame->mHeight) { |
| 290 | size_t displayWidth = videoFrame->mDisplayWidth; |
| 291 | size_t displayHeight = videoFrame->mDisplayHeight; |
| 292 | if (swapWidthAndHeight) { |
| 293 | displayWidth = videoFrame->mDisplayHeight; |
| 294 | displayHeight = videoFrame->mDisplayWidth; |
| 295 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 296 | ALOGV("Bitmap dimension is scaled from %dx%d to %dx%d", |
James Dong | 9f2cde3 | 2011-03-18 17:55:06 -0700 | [diff] [blame] | 297 | width, height, displayWidth, displayHeight); |
| 298 | jobject scaledBitmap = env->CallStaticObjectMethod(fields.bitmapClazz, |
| 299 | fields.createScaledBitmapMethod, |
| 300 | jBitmap, |
| 301 | displayWidth, |
| 302 | displayHeight, |
| 303 | true); |
| 304 | return scaledBitmap; |
| 305 | } |
| 306 | |
James Dong | 0e4b535 | 2010-12-19 13:05:33 -0800 | [diff] [blame] | 307 | return jBitmap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | } |
| 309 | |
James Dong | df9b349 | 2011-01-04 15:03:48 -0800 | [diff] [blame] | 310 | static jbyteArray android_media_MediaMetadataRetriever_getEmbeddedPicture( |
| 311 | JNIEnv *env, jobject thiz, jint pictureType) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 313 | ALOGV("getEmbeddedPicture: %d", pictureType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | MediaMetadataRetriever* retriever = getRetriever(env, thiz); |
| 315 | if (retriever == 0) { |
| 316 | jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); |
| 317 | return NULL; |
| 318 | } |
| 319 | MediaAlbumArt* mediaAlbumArt = NULL; |
James Dong | df9b349 | 2011-01-04 15:03:48 -0800 | [diff] [blame] | 320 | |
| 321 | // FIXME: |
| 322 | // Use pictureType to retrieve the intended embedded picture and also change |
| 323 | // the method name to getEmbeddedPicture(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | sp<IMemory> albumArtMemory = retriever->extractAlbumArt(); |
| 325 | if (albumArtMemory != 0) { // cast the shared structure to a MediaAlbumArt object |
| 326 | mediaAlbumArt = static_cast<MediaAlbumArt *>(albumArtMemory->pointer()); |
| 327 | } |
| 328 | if (mediaAlbumArt == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 329 | ALOGE("getEmbeddedPicture: Call to getEmbeddedPicture failed."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | return NULL; |
| 331 | } |
| 332 | |
| 333 | unsigned int len = mediaAlbumArt->mSize; |
| 334 | char* data = (char*) mediaAlbumArt + sizeof(MediaAlbumArt); |
| 335 | jbyteArray array = env->NewByteArray(len); |
| 336 | if (!array) { // OutOfMemoryError exception has already been thrown. |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 337 | ALOGE("getEmbeddedPicture: OutOfMemoryError is thrown."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | } else { |
| 339 | jbyte* bytes = env->GetByteArrayElements(array, NULL); |
| 340 | if (bytes != NULL) { |
| 341 | memcpy(bytes, data, len); |
| 342 | env->ReleaseByteArrayElements(array, bytes, 0); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | // No need to delete mediaAlbumArt here |
| 347 | return array; |
| 348 | } |
| 349 | |
| 350 | static jobject android_media_MediaMetadataRetriever_extractMetadata(JNIEnv *env, jobject thiz, jint keyCode) |
| 351 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 352 | ALOGV("extractMetadata"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | MediaMetadataRetriever* retriever = getRetriever(env, thiz); |
| 354 | if (retriever == 0) { |
| 355 | jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); |
| 356 | return NULL; |
| 357 | } |
| 358 | const char* value = retriever->extractMetadata(keyCode); |
| 359 | if (!value) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 360 | ALOGV("extractMetadata: Metadata is not found"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | return NULL; |
| 362 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 363 | ALOGV("extractMetadata: value (%s) for keyCode(%d)", value, keyCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | return env->NewStringUTF(value); |
| 365 | } |
| 366 | |
| 367 | static void android_media_MediaMetadataRetriever_release(JNIEnv *env, jobject thiz) |
| 368 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 369 | ALOGV("release"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | Mutex::Autolock lock(sLock); |
| 371 | MediaMetadataRetriever* retriever = getRetriever(env, thiz); |
| 372 | delete retriever; |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 373 | setRetriever(env, thiz, (MediaMetadataRetriever*) 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | static void android_media_MediaMetadataRetriever_native_finalize(JNIEnv *env, jobject thiz) |
| 377 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 378 | ALOGV("native_finalize"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | // No lock is needed, since android_media_MediaMetadataRetriever_release() is protected |
| 380 | android_media_MediaMetadataRetriever_release(env, thiz); |
| 381 | } |
| 382 | |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 383 | // This function gets a field ID, which in turn causes class initialization. |
| 384 | // It is called from a static block in MediaMetadataRetriever, which won't run until the |
| 385 | // first time an instance of this class is used. |
| 386 | static void android_media_MediaMetadataRetriever_native_init(JNIEnv *env) |
| 387 | { |
| 388 | jclass clazz = env->FindClass(kClassPathName); |
| 389 | if (clazz == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 390 | return; |
| 391 | } |
| 392 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 393 | fields.context = env->GetFieldID(clazz, "mNativeContext", "J"); |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 394 | if (fields.context == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 395 | return; |
| 396 | } |
| 397 | |
Brian Carlstrom | 46e18c11 | 2011-04-05 22:44:45 -0700 | [diff] [blame] | 398 | jclass bitmapClazz = env->FindClass("android/graphics/Bitmap"); |
| 399 | if (bitmapClazz == NULL) { |
| 400 | return; |
| 401 | } |
| 402 | fields.bitmapClazz = (jclass) env->NewGlobalRef(bitmapClazz); |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 403 | if (fields.bitmapClazz == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 404 | return; |
| 405 | } |
James Dong | 0e4b535 | 2010-12-19 13:05:33 -0800 | [diff] [blame] | 406 | fields.createBitmapMethod = |
| 407 | env->GetStaticMethodID(fields.bitmapClazz, "createBitmap", |
| 408 | "(IILandroid/graphics/Bitmap$Config;)" |
| 409 | "Landroid/graphics/Bitmap;"); |
| 410 | if (fields.createBitmapMethod == NULL) { |
James Dong | 0e4b535 | 2010-12-19 13:05:33 -0800 | [diff] [blame] | 411 | return; |
| 412 | } |
James Dong | 9f2cde3 | 2011-03-18 17:55:06 -0700 | [diff] [blame] | 413 | fields.createScaledBitmapMethod = |
| 414 | env->GetStaticMethodID(fields.bitmapClazz, "createScaledBitmap", |
| 415 | "(Landroid/graphics/Bitmap;IIZ)" |
| 416 | "Landroid/graphics/Bitmap;"); |
| 417 | if (fields.createScaledBitmapMethod == NULL) { |
James Dong | 9f2cde3 | 2011-03-18 17:55:06 -0700 | [diff] [blame] | 418 | return; |
| 419 | } |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 420 | fields.nativeBitmap = env->GetFieldID(fields.bitmapClazz, "mNativeBitmap", "J"); |
James Dong | 0e4b535 | 2010-12-19 13:05:33 -0800 | [diff] [blame] | 421 | if (fields.nativeBitmap == NULL) { |
Brian Carlstrom | 46e18c11 | 2011-04-05 22:44:45 -0700 | [diff] [blame] | 422 | return; |
James Dong | 0e4b535 | 2010-12-19 13:05:33 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Brian Carlstrom | 46e18c11 | 2011-04-05 22:44:45 -0700 | [diff] [blame] | 425 | jclass configClazz = env->FindClass("android/graphics/Bitmap$Config"); |
| 426 | if (configClazz == NULL) { |
| 427 | return; |
| 428 | } |
| 429 | fields.configClazz = (jclass) env->NewGlobalRef(configClazz); |
James Dong | 0e4b535 | 2010-12-19 13:05:33 -0800 | [diff] [blame] | 430 | if (fields.configClazz == NULL) { |
James Dong | 0e4b535 | 2010-12-19 13:05:33 -0800 | [diff] [blame] | 431 | return; |
| 432 | } |
| 433 | fields.createConfigMethod = |
| 434 | env->GetStaticMethodID(fields.configClazz, "nativeToConfig", |
| 435 | "(I)Landroid/graphics/Bitmap$Config;"); |
| 436 | if (fields.createConfigMethod == NULL) { |
James Dong | 0e4b535 | 2010-12-19 13:05:33 -0800 | [diff] [blame] | 437 | return; |
| 438 | } |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 439 | } |
| 440 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | static void android_media_MediaMetadataRetriever_native_setup(JNIEnv *env, jobject thiz) |
| 442 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 443 | ALOGV("native_setup"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | MediaMetadataRetriever* retriever = new MediaMetadataRetriever(); |
| 445 | if (retriever == 0) { |
| 446 | jniThrowException(env, "java/lang/RuntimeException", "Out of memory"); |
| 447 | return; |
| 448 | } |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 449 | setRetriever(env, thiz, retriever); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | // JNI mapping between Java methods and native methods |
| 453 | static JNINativeMethod nativeMethods[] = { |
James Dong | 17524dc | 2011-05-04 13:41:58 -0700 | [diff] [blame] | 454 | { |
| 455 | "_setDataSource", |
Andreas Huber | d2506a5 | 2014-01-29 10:32:46 -0800 | [diff] [blame] | 456 | "(Landroid/os/IBinder;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)V", |
James Dong | 17524dc | 2011-05-04 13:41:58 -0700 | [diff] [blame] | 457 | (void *)android_media_MediaMetadataRetriever_setDataSourceAndHeaders |
| 458 | }, |
| 459 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | {"setDataSource", "(Ljava/io/FileDescriptor;JJ)V", (void *)android_media_MediaMetadataRetriever_setDataSourceFD}, |
James Dong | faf09ba | 2010-12-02 17:42:08 -0800 | [diff] [blame] | 461 | {"_getFrameAtTime", "(JI)Landroid/graphics/Bitmap;", (void *)android_media_MediaMetadataRetriever_getFrameAtTime}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | {"extractMetadata", "(I)Ljava/lang/String;", (void *)android_media_MediaMetadataRetriever_extractMetadata}, |
James Dong | df9b349 | 2011-01-04 15:03:48 -0800 | [diff] [blame] | 463 | {"getEmbeddedPicture", "(I)[B", (void *)android_media_MediaMetadataRetriever_getEmbeddedPicture}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | {"release", "()V", (void *)android_media_MediaMetadataRetriever_release}, |
| 465 | {"native_finalize", "()V", (void *)android_media_MediaMetadataRetriever_native_finalize}, |
| 466 | {"native_setup", "()V", (void *)android_media_MediaMetadataRetriever_native_setup}, |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 467 | {"native_init", "()V", (void *)android_media_MediaMetadataRetriever_native_init}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 468 | }; |
| 469 | |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 470 | // This function only registers the native methods, and is called from |
| 471 | // JNI_OnLoad in android_media_MediaPlayer.cpp |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | int register_android_media_MediaMetadataRetriever(JNIEnv *env) |
| 473 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | return AndroidRuntime::registerNativeMethods |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 475 | (env, kClassPathName, nativeMethods, NELEM(nativeMethods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | } |