2 * Copyright 2013, The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "MediaMuxer-JNI"
19 #include <utils/Log.h>
21 #include "android_media_Utils.h"
22 #include "android_runtime/AndroidRuntime.h"
26 #include <media/stagefright/foundation/ABuffer.h>
27 #include <media/stagefright/foundation/ADebug.h>
28 #include <media/stagefright/foundation/AMessage.h>
29 #include <media/stagefright/MediaMuxer.h>
37 static fields_t gFields;
41 using namespace android;
43 static jint android_media_MediaMuxer_addTrack(
44 JNIEnv *env, jclass /* clazz */, jlong nativeObject, jobjectArray keys,
45 jobjectArray values) {
46 sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject));
48 jniThrowException(env, "java/lang/IllegalStateException",
49 "Muxer was not set up correctly");
53 sp<AMessage> trackformat;
54 status_t err = ConvertKeyValueArraysToMessage(env, keys, values,
57 jniThrowException(env, "java/lang/IllegalArgumentException",
58 "ConvertKeyValueArraysToMessage got an error");
62 // Return negative value when errors happen in addTrack.
63 jint trackIndex = muxer->addTrack(trackformat);
66 jniThrowException(env, "java/lang/IllegalStateException",
67 "Failed to add the track to the muxer");
73 static void android_media_MediaMuxer_writeSampleData(
74 JNIEnv *env, jclass /* clazz */, jlong nativeObject, jint trackIndex,
75 jobject byteBuf, jint offset, jint size, jlong timeUs, jint flags) {
76 sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject));
78 jniThrowException(env, "java/lang/IllegalStateException",
79 "Muxer was not set up correctly");
83 // Try to convert the incoming byteBuffer into ABuffer
84 void *dst = env->GetDirectBufferAddress(byteBuf);
87 jbyteArray byteArray = NULL;
92 (jbyteArray)env->CallObjectMethod(byteBuf, gFields.arrayID);
94 if (byteArray == NULL) {
95 jniThrowException(env, "java/lang/IllegalArgumentException",
101 dst = env->GetByteArrayElements(byteArray, &isCopy);
103 dstSize = env->GetArrayLength(byteArray);
105 dstSize = env->GetDirectBufferCapacity(byteBuf);
108 if (dstSize < (offset + size)) {
109 ALOGE("writeSampleData saw wrong dstSize %lld, size %d, offset %d",
110 (long long)dstSize, size, offset);
111 if (byteArray != NULL) {
112 env->ReleaseByteArrayElements(byteArray, (jbyte *)dst, 0);
114 jniThrowException(env, "java/lang/IllegalArgumentException",
115 "sample has a wrong size");
119 sp<ABuffer> buffer = new ABuffer((char *)dst + offset, size);
121 status_t err = muxer->writeSampleData(buffer, trackIndex, timeUs, flags);
123 if (byteArray != NULL) {
124 env->ReleaseByteArrayElements(byteArray, (jbyte *)dst, 0);
128 jniThrowException(env, "java/lang/IllegalStateException",
129 "writeSampleData returned an error");
134 // Constructor counterpart.
135 static jlong android_media_MediaMuxer_native_setup(
136 JNIEnv *env, jclass clazz, jobject fileDescriptor,
138 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
139 ALOGV("native_setup: fd %d", fd);
141 MediaMuxer::OutputFormat fileFormat =
142 static_cast<MediaMuxer::OutputFormat>(format);
143 sp<MediaMuxer> muxer = new MediaMuxer(fd, fileFormat);
144 muxer->incStrong(clazz);
145 return reinterpret_cast<jlong>(muxer.get());
148 static void android_media_MediaMuxer_setOrientationHint(
149 JNIEnv *env, jclass /* clazz */, jlong nativeObject, jint degrees) {
150 sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject));
152 jniThrowException(env, "java/lang/IllegalStateException",
153 "Muxer was not set up correctly");
156 status_t err = muxer->setOrientationHint(degrees);
159 jniThrowException(env, "java/lang/IllegalStateException",
160 "Failed to set orientation hint");
166 static void android_media_MediaMuxer_setLocation(
167 JNIEnv *env, jclass /* clazz */, jlong nativeObject, jint latitude, jint longitude) {
168 MediaMuxer* muxer = reinterpret_cast<MediaMuxer *>(nativeObject);
170 status_t res = muxer->setLocation(latitude, longitude);
172 jniThrowException(env, "java/lang/IllegalStateException",
173 "Failed to set location");
178 static void android_media_MediaMuxer_start(JNIEnv *env, jclass /* clazz */,
179 jlong nativeObject) {
180 sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject));
182 jniThrowException(env, "java/lang/IllegalStateException",
183 "Muxer was not set up correctly");
186 status_t err = muxer->start();
189 jniThrowException(env, "java/lang/IllegalStateException",
190 "Failed to start the muxer");
196 static void android_media_MediaMuxer_stop(JNIEnv *env, jclass /* clazz */,
197 jlong nativeObject) {
198 sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject));
200 jniThrowException(env, "java/lang/IllegalStateException",
201 "Muxer was not set up correctly");
205 status_t err = muxer->stop();
208 jniThrowException(env, "java/lang/IllegalStateException",
209 "Failed to stop the muxer");
214 static void android_media_MediaMuxer_native_release(
215 JNIEnv* /* env */, jclass clazz, jlong nativeObject) {
216 sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject));
218 muxer->decStrong(clazz);
222 static JNINativeMethod gMethods[] = {
224 { "nativeAddTrack", "(J[Ljava/lang/String;[Ljava/lang/Object;)I",
225 (void *)android_media_MediaMuxer_addTrack },
227 { "nativeSetOrientationHint", "(JI)V",
228 (void *)android_media_MediaMuxer_setOrientationHint},
230 { "nativeSetLocation", "(JII)V",
231 (void *)android_media_MediaMuxer_setLocation},
233 { "nativeStart", "(J)V", (void *)android_media_MediaMuxer_start},
235 { "nativeWriteSampleData", "(JILjava/nio/ByteBuffer;IIJI)V",
236 (void *)android_media_MediaMuxer_writeSampleData },
238 { "nativeStop", "(J)V", (void *)android_media_MediaMuxer_stop},
240 { "nativeSetup", "(Ljava/io/FileDescriptor;I)J",
241 (void *)android_media_MediaMuxer_native_setup },
243 { "nativeRelease", "(J)V",
244 (void *)android_media_MediaMuxer_native_release },
248 // This function only registers the native methods, and is called from
249 // JNI_OnLoad in android_media_MediaPlayer.cpp
250 int register_android_media_MediaMuxer(JNIEnv *env) {
251 int err = AndroidRuntime::registerNativeMethods(env,
252 "android/media/MediaMuxer", gMethods, NELEM(gMethods));
254 jclass byteBufClass = env->FindClass("java/nio/ByteBuffer");
255 CHECK(byteBufClass != NULL);
258 env->GetMethodID(byteBufClass, "array", "()[B");
259 CHECK(gFields.arrayID != NULL);