ztenghui | 68ccf10 | 2013-02-13 14:07:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "MediaMuxer-JNI" |
| 19 | #include <utils/Log.h> |
| 20 | |
Jooyung Han | cb1e896 | 2019-02-21 14:18:11 +0900 | [diff] [blame] | 21 | #include "android_media_Streams.h" |
ztenghui | 68ccf10 | 2013-02-13 14:07:02 -0800 | [diff] [blame] | 22 | #include "android_runtime/AndroidRuntime.h" |
| 23 | #include "jni.h" |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 24 | #include <nativehelper/JNIHelp.h> |
ztenghui | 68ccf10 | 2013-02-13 14:07:02 -0800 | [diff] [blame] | 25 | |
Hangyu Kuang | 61c74c6a | 2017-01-20 10:15:04 -0800 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | #include <fcntl.h> |
| 28 | |
ztenghui | 68ccf10 | 2013-02-13 14:07:02 -0800 | [diff] [blame] | 29 | #include <media/stagefright/foundation/ABuffer.h> |
| 30 | #include <media/stagefright/foundation/ADebug.h> |
| 31 | #include <media/stagefright/foundation/AMessage.h> |
| 32 | #include <media/stagefright/MediaMuxer.h> |
| 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | struct fields_t { |
ztenghui | 68ccf10 | 2013-02-13 14:07:02 -0800 | [diff] [blame] | 37 | jmethodID arrayID; |
| 38 | }; |
| 39 | |
| 40 | static fields_t gFields; |
| 41 | |
| 42 | } |
| 43 | |
| 44 | using namespace android; |
| 45 | |
| 46 | static jint android_media_MediaMuxer_addTrack( |
Andreas Gampe | 5a15d0d | 2014-11-10 18:19:40 -0800 | [diff] [blame] | 47 | JNIEnv *env, jclass /* clazz */, jlong nativeObject, jobjectArray keys, |
ztenghui | 68ccf10 | 2013-02-13 14:07:02 -0800 | [diff] [blame] | 48 | jobjectArray values) { |
| 49 | sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject)); |
| 50 | if (muxer == NULL) { |
| 51 | jniThrowException(env, "java/lang/IllegalStateException", |
| 52 | "Muxer was not set up correctly"); |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | sp<AMessage> trackformat; |
| 57 | status_t err = ConvertKeyValueArraysToMessage(env, keys, values, |
| 58 | &trackformat); |
| 59 | if (err != OK) { |
| 60 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 61 | "ConvertKeyValueArraysToMessage got an error"); |
| 62 | return err; |
| 63 | } |
| 64 | |
| 65 | // Return negative value when errors happen in addTrack. |
| 66 | jint trackIndex = muxer->addTrack(trackformat); |
| 67 | |
| 68 | if (trackIndex < 0) { |
| 69 | jniThrowException(env, "java/lang/IllegalStateException", |
| 70 | "Failed to add the track to the muxer"); |
| 71 | return -1; |
| 72 | } |
| 73 | return trackIndex; |
| 74 | } |
| 75 | |
| 76 | static void android_media_MediaMuxer_writeSampleData( |
Andreas Gampe | 5a15d0d | 2014-11-10 18:19:40 -0800 | [diff] [blame] | 77 | JNIEnv *env, jclass /* clazz */, jlong nativeObject, jint trackIndex, |
ztenghui | 68ccf10 | 2013-02-13 14:07:02 -0800 | [diff] [blame] | 78 | jobject byteBuf, jint offset, jint size, jlong timeUs, jint flags) { |
| 79 | sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject)); |
| 80 | if (muxer == NULL) { |
| 81 | jniThrowException(env, "java/lang/IllegalStateException", |
| 82 | "Muxer was not set up correctly"); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | // Try to convert the incoming byteBuffer into ABuffer |
| 87 | void *dst = env->GetDirectBufferAddress(byteBuf); |
| 88 | |
| 89 | jlong dstSize; |
| 90 | jbyteArray byteArray = NULL; |
| 91 | |
| 92 | if (dst == NULL) { |
| 93 | |
| 94 | byteArray = |
| 95 | (jbyteArray)env->CallObjectMethod(byteBuf, gFields.arrayID); |
| 96 | |
| 97 | if (byteArray == NULL) { |
| 98 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 99 | "byteArray is null"); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | jboolean isCopy; |
| 104 | dst = env->GetByteArrayElements(byteArray, &isCopy); |
| 105 | |
| 106 | dstSize = env->GetArrayLength(byteArray); |
| 107 | } else { |
| 108 | dstSize = env->GetDirectBufferCapacity(byteBuf); |
| 109 | } |
| 110 | |
| 111 | if (dstSize < (offset + size)) { |
| 112 | ALOGE("writeSampleData saw wrong dstSize %lld, size %d, offset %d", |
Bernhard Rosenkränzer | 46c82b4 | 2014-11-30 11:04:10 +0100 | [diff] [blame] | 113 | (long long)dstSize, size, offset); |
ztenghui | 68ccf10 | 2013-02-13 14:07:02 -0800 | [diff] [blame] | 114 | if (byteArray != NULL) { |
| 115 | env->ReleaseByteArrayElements(byteArray, (jbyte *)dst, 0); |
| 116 | } |
| 117 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 118 | "sample has a wrong size"); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | sp<ABuffer> buffer = new ABuffer((char *)dst + offset, size); |
| 123 | |
| 124 | status_t err = muxer->writeSampleData(buffer, trackIndex, timeUs, flags); |
| 125 | |
| 126 | if (byteArray != NULL) { |
| 127 | env->ReleaseByteArrayElements(byteArray, (jbyte *)dst, 0); |
| 128 | } |
| 129 | |
| 130 | if (err != OK) { |
| 131 | jniThrowException(env, "java/lang/IllegalStateException", |
| 132 | "writeSampleData returned an error"); |
| 133 | } |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | // Constructor counterpart. |
Ashok Bhat | d2507a2 | 2014-02-07 12:22:50 +0000 | [diff] [blame] | 138 | static jlong android_media_MediaMuxer_native_setup( |
ztenghui | 68ccf10 | 2013-02-13 14:07:02 -0800 | [diff] [blame] | 139 | JNIEnv *env, jclass clazz, jobject fileDescriptor, |
| 140 | jint format) { |
| 141 | int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
| 142 | ALOGV("native_setup: fd %d", fd); |
| 143 | |
Hangyu Kuang | 61c74c6a | 2017-01-20 10:15:04 -0800 | [diff] [blame] | 144 | // It appears that if an invalid file descriptor is passed through |
| 145 | // binder calls, the server-side of the inter-process function call |
| 146 | // is skipped. As a result, the check at the server-side to catch |
| 147 | // the invalid file descritpor never gets invoked. This is to workaround |
| 148 | // this issue by checking the file descriptor first before passing |
| 149 | // it through binder call. |
| 150 | int flags = fcntl(fd, F_GETFL); |
| 151 | if (flags == -1) { |
| 152 | ALOGE("Fail to get File Status Flags err: %s", strerror(errno)); |
| 153 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 154 | "Invalid file descriptor"); |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | // fd must be in read-write mode or write-only mode. |
| 159 | if ((flags & (O_RDWR | O_WRONLY)) == 0) { |
| 160 | ALOGE("File descriptor is not in read-write mode or write-only mode"); |
| 161 | jniThrowException(env, "java/io/IOException", |
| 162 | "File descriptor is not in read-write mode or write-only mode"); |
| 163 | return 0; |
| 164 | } |
| 165 | |
ztenghui | 68ccf10 | 2013-02-13 14:07:02 -0800 | [diff] [blame] | 166 | MediaMuxer::OutputFormat fileFormat = |
| 167 | static_cast<MediaMuxer::OutputFormat>(format); |
| 168 | sp<MediaMuxer> muxer = new MediaMuxer(fd, fileFormat); |
| 169 | muxer->incStrong(clazz); |
Ashok Bhat | d2507a2 | 2014-02-07 12:22:50 +0000 | [diff] [blame] | 170 | return reinterpret_cast<jlong>(muxer.get()); |
ztenghui | 68ccf10 | 2013-02-13 14:07:02 -0800 | [diff] [blame] | 171 | } |
| 172 | |
ztenghui | effc9b4 | 2013-03-12 16:03:12 -0700 | [diff] [blame] | 173 | static void android_media_MediaMuxer_setOrientationHint( |
Andreas Gampe | 5a15d0d | 2014-11-10 18:19:40 -0800 | [diff] [blame] | 174 | JNIEnv *env, jclass /* clazz */, jlong nativeObject, jint degrees) { |
ztenghui | effc9b4 | 2013-03-12 16:03:12 -0700 | [diff] [blame] | 175 | sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject)); |
| 176 | if (muxer == NULL) { |
| 177 | jniThrowException(env, "java/lang/IllegalStateException", |
| 178 | "Muxer was not set up correctly"); |
| 179 | return; |
| 180 | } |
| 181 | status_t err = muxer->setOrientationHint(degrees); |
| 182 | |
| 183 | if (err != OK) { |
| 184 | jniThrowException(env, "java/lang/IllegalStateException", |
| 185 | "Failed to set orientation hint"); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | } |
| 190 | |
Zhijun He | cfd4748 | 2013-09-09 15:47:06 -0700 | [diff] [blame] | 191 | static void android_media_MediaMuxer_setLocation( |
Andreas Gampe | 5a15d0d | 2014-11-10 18:19:40 -0800 | [diff] [blame] | 192 | JNIEnv *env, jclass /* clazz */, jlong nativeObject, jint latitude, jint longitude) { |
Zhijun He | cfd4748 | 2013-09-09 15:47:06 -0700 | [diff] [blame] | 193 | MediaMuxer* muxer = reinterpret_cast<MediaMuxer *>(nativeObject); |
| 194 | |
| 195 | status_t res = muxer->setLocation(latitude, longitude); |
| 196 | if (res != OK) { |
| 197 | jniThrowException(env, "java/lang/IllegalStateException", |
| 198 | "Failed to set location"); |
| 199 | return; |
| 200 | } |
| 201 | } |
| 202 | |
Andreas Gampe | 5a15d0d | 2014-11-10 18:19:40 -0800 | [diff] [blame] | 203 | static void android_media_MediaMuxer_start(JNIEnv *env, jclass /* clazz */, |
Ashok Bhat | 656fd04 | 2013-11-28 10:56:06 +0000 | [diff] [blame] | 204 | jlong nativeObject) { |
ztenghui | 68ccf10 | 2013-02-13 14:07:02 -0800 | [diff] [blame] | 205 | sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject)); |
| 206 | if (muxer == NULL) { |
| 207 | jniThrowException(env, "java/lang/IllegalStateException", |
| 208 | "Muxer was not set up correctly"); |
| 209 | return; |
| 210 | } |
| 211 | status_t err = muxer->start(); |
| 212 | |
| 213 | if (err != OK) { |
| 214 | jniThrowException(env, "java/lang/IllegalStateException", |
| 215 | "Failed to start the muxer"); |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | } |
| 220 | |
Andreas Gampe | 5a15d0d | 2014-11-10 18:19:40 -0800 | [diff] [blame] | 221 | static void android_media_MediaMuxer_stop(JNIEnv *env, jclass /* clazz */, |
Ashok Bhat | 656fd04 | 2013-11-28 10:56:06 +0000 | [diff] [blame] | 222 | jlong nativeObject) { |
ztenghui | 68ccf10 | 2013-02-13 14:07:02 -0800 | [diff] [blame] | 223 | sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject)); |
| 224 | if (muxer == NULL) { |
| 225 | jniThrowException(env, "java/lang/IllegalStateException", |
| 226 | "Muxer was not set up correctly"); |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | status_t err = muxer->stop(); |
| 231 | |
| 232 | if (err != OK) { |
| 233 | jniThrowException(env, "java/lang/IllegalStateException", |
| 234 | "Failed to stop the muxer"); |
| 235 | return; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | static void android_media_MediaMuxer_native_release( |
Andreas Gampe | 5a15d0d | 2014-11-10 18:19:40 -0800 | [diff] [blame] | 240 | JNIEnv* /* env */, jclass clazz, jlong nativeObject) { |
ztenghui | 68ccf10 | 2013-02-13 14:07:02 -0800 | [diff] [blame] | 241 | sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject)); |
| 242 | if (muxer != NULL) { |
| 243 | muxer->decStrong(clazz); |
| 244 | } |
| 245 | } |
| 246 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 247 | static const JNINativeMethod gMethods[] = { |
ztenghui | 68ccf10 | 2013-02-13 14:07:02 -0800 | [diff] [blame] | 248 | |
Ashok Bhat | 656fd04 | 2013-11-28 10:56:06 +0000 | [diff] [blame] | 249 | { "nativeAddTrack", "(J[Ljava/lang/String;[Ljava/lang/Object;)I", |
ztenghui | 68ccf10 | 2013-02-13 14:07:02 -0800 | [diff] [blame] | 250 | (void *)android_media_MediaMuxer_addTrack }, |
| 251 | |
Ashok Bhat | 656fd04 | 2013-11-28 10:56:06 +0000 | [diff] [blame] | 252 | { "nativeSetOrientationHint", "(JI)V", |
ztenghui | effc9b4 | 2013-03-12 16:03:12 -0700 | [diff] [blame] | 253 | (void *)android_media_MediaMuxer_setOrientationHint}, |
| 254 | |
Ashok Bhat | 656fd04 | 2013-11-28 10:56:06 +0000 | [diff] [blame] | 255 | { "nativeSetLocation", "(JII)V", |
Zhijun He | cfd4748 | 2013-09-09 15:47:06 -0700 | [diff] [blame] | 256 | (void *)android_media_MediaMuxer_setLocation}, |
| 257 | |
Ashok Bhat | 656fd04 | 2013-11-28 10:56:06 +0000 | [diff] [blame] | 258 | { "nativeStart", "(J)V", (void *)android_media_MediaMuxer_start}, |
ztenghui | 68ccf10 | 2013-02-13 14:07:02 -0800 | [diff] [blame] | 259 | |
Ashok Bhat | 656fd04 | 2013-11-28 10:56:06 +0000 | [diff] [blame] | 260 | { "nativeWriteSampleData", "(JILjava/nio/ByteBuffer;IIJI)V", |
ztenghui | 68ccf10 | 2013-02-13 14:07:02 -0800 | [diff] [blame] | 261 | (void *)android_media_MediaMuxer_writeSampleData }, |
| 262 | |
Ashok Bhat | 656fd04 | 2013-11-28 10:56:06 +0000 | [diff] [blame] | 263 | { "nativeStop", "(J)V", (void *)android_media_MediaMuxer_stop}, |
ztenghui | 68ccf10 | 2013-02-13 14:07:02 -0800 | [diff] [blame] | 264 | |
Ashok Bhat | 656fd04 | 2013-11-28 10:56:06 +0000 | [diff] [blame] | 265 | { "nativeSetup", "(Ljava/io/FileDescriptor;I)J", |
ztenghui | 68ccf10 | 2013-02-13 14:07:02 -0800 | [diff] [blame] | 266 | (void *)android_media_MediaMuxer_native_setup }, |
| 267 | |
Ashok Bhat | 656fd04 | 2013-11-28 10:56:06 +0000 | [diff] [blame] | 268 | { "nativeRelease", "(J)V", |
ztenghui | 68ccf10 | 2013-02-13 14:07:02 -0800 | [diff] [blame] | 269 | (void *)android_media_MediaMuxer_native_release }, |
| 270 | |
| 271 | }; |
| 272 | |
| 273 | // This function only registers the native methods, and is called from |
| 274 | // JNI_OnLoad in android_media_MediaPlayer.cpp |
| 275 | int register_android_media_MediaMuxer(JNIEnv *env) { |
| 276 | int err = AndroidRuntime::registerNativeMethods(env, |
| 277 | "android/media/MediaMuxer", gMethods, NELEM(gMethods)); |
| 278 | |
ztenghui | 68ccf10 | 2013-02-13 14:07:02 -0800 | [diff] [blame] | 279 | jclass byteBufClass = env->FindClass("java/nio/ByteBuffer"); |
| 280 | CHECK(byteBufClass != NULL); |
| 281 | |
| 282 | gFields.arrayID = |
| 283 | env->GetMethodID(byteBufClass, "array", "()[B"); |
| 284 | CHECK(gFields.arrayID != NULL); |
| 285 | |
| 286 | return err; |
| 287 | } |