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