android_media_MediaMuxer.cpp revision 68ccf103a10c674f1db649bb15bb3e790bc6dad3
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
21#include "android_media_Utils.h"
22#include "android_runtime/AndroidRuntime.h"
23#include "jni.h"
24#include "JNIHelp.h"
25
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>
30
31namespace android {
32
33struct fields_t {
34    jfieldID context;
35    jmethodID arrayID;
36};
37
38static fields_t gFields;
39
40}
41
42using namespace android;
43
44static jint android_media_MediaMuxer_addTrack(
45        JNIEnv *env, jclass clazz, jint nativeObject, jobjectArray keys,
46        jobjectArray values) {
47    sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject));
48    if (muxer == NULL) {
49        jniThrowException(env, "java/lang/IllegalStateException",
50                          "Muxer was not set up correctly");
51        return -1;
52    }
53
54    sp<AMessage> trackformat;
55    status_t err = ConvertKeyValueArraysToMessage(env, keys, values,
56                                                  &trackformat);
57    if (err != OK) {
58        jniThrowException(env, "java/lang/IllegalArgumentException",
59                          "ConvertKeyValueArraysToMessage got an error");
60        return err;
61    }
62
63    // Return negative value when errors happen in addTrack.
64    jint trackIndex = muxer->addTrack(trackformat);
65
66    if (trackIndex < 0) {
67        jniThrowException(env, "java/lang/IllegalStateException",
68                          "Failed to add the track to the muxer");
69        return -1;
70    }
71    return trackIndex;
72}
73
74static void android_media_MediaMuxer_writeSampleData(
75        JNIEnv *env, jclass clazz, jint nativeObject, jint trackIndex,
76        jobject byteBuf, jint offset, jint size, jlong timeUs, jint flags) {
77    sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject));
78    if (muxer == NULL) {
79        jniThrowException(env, "java/lang/IllegalStateException",
80                          "Muxer was not set up correctly");
81        return;
82    }
83
84    // Try to convert the incoming byteBuffer into ABuffer
85    void *dst = env->GetDirectBufferAddress(byteBuf);
86
87    jlong dstSize;
88    jbyteArray byteArray = NULL;
89
90    if (dst == NULL) {
91
92        byteArray =
93            (jbyteArray)env->CallObjectMethod(byteBuf, gFields.arrayID);
94
95        if (byteArray == NULL) {
96            jniThrowException(env, "java/lang/IllegalArgumentException",
97                              "byteArray is null");
98            return;
99        }
100
101        jboolean isCopy;
102        dst = env->GetByteArrayElements(byteArray, &isCopy);
103
104        dstSize = env->GetArrayLength(byteArray);
105    } else {
106        dstSize = env->GetDirectBufferCapacity(byteBuf);
107    }
108
109    if (dstSize < (offset + size)) {
110        ALOGE("writeSampleData saw wrong dstSize %lld, size  %d, offset %d",
111              dstSize, size, offset);
112        if (byteArray != NULL) {
113            env->ReleaseByteArrayElements(byteArray, (jbyte *)dst, 0);
114        }
115        jniThrowException(env, "java/lang/IllegalArgumentException",
116                          "sample has a wrong size");
117        return;
118    }
119
120    sp<ABuffer> buffer = new ABuffer((char *)dst + offset, size);
121
122    status_t err = muxer->writeSampleData(buffer, trackIndex, timeUs, flags);
123
124    if (byteArray != NULL) {
125        env->ReleaseByteArrayElements(byteArray, (jbyte *)dst, 0);
126    }
127
128    if (err != OK) {
129        jniThrowException(env, "java/lang/IllegalStateException",
130                          "writeSampleData returned an error");
131    }
132    return;
133}
134
135// Constructor counterpart.
136static jint android_media_MediaMuxer_native_setup(
137        JNIEnv *env, jclass clazz, jobject fileDescriptor,
138        jint format) {
139    int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
140    ALOGV("native_setup: fd %d", fd);
141
142    MediaMuxer::OutputFormat fileFormat =
143        static_cast<MediaMuxer::OutputFormat>(format);
144    sp<MediaMuxer> muxer = new MediaMuxer(fd, fileFormat);
145    muxer->incStrong(clazz);
146    return int(muxer.get());
147}
148
149static void android_media_MediaMuxer_start(JNIEnv *env, jclass clazz,
150                                           jint nativeObject) {
151    sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject));
152    if (muxer == NULL) {
153        jniThrowException(env, "java/lang/IllegalStateException",
154                          "Muxer was not set up correctly");
155        return;
156    }
157    status_t err = muxer->start();
158
159    if (err != OK) {
160        jniThrowException(env, "java/lang/IllegalStateException",
161                          "Failed to start the muxer");
162        return;
163    }
164
165}
166
167static void android_media_MediaMuxer_stop(JNIEnv *env, jclass clazz,
168                                          jint nativeObject) {
169    sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject));
170    if (muxer == NULL) {
171        jniThrowException(env, "java/lang/IllegalStateException",
172                          "Muxer was not set up correctly");
173        return;
174    }
175
176    status_t err = muxer->stop();
177
178    if (err != OK) {
179        jniThrowException(env, "java/lang/IllegalStateException",
180                          "Failed to stop the muxer");
181        return;
182    }
183}
184
185static void android_media_MediaMuxer_native_release(
186        JNIEnv *env, jclass clazz, jint nativeObject) {
187    sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject));
188    if (muxer != NULL) {
189        muxer->decStrong(clazz);
190    }
191}
192
193static JNINativeMethod gMethods[] = {
194
195    { "nativeAddTrack", "(I[Ljava/lang/String;[Ljava/lang/Object;)I",
196        (void *)android_media_MediaMuxer_addTrack },
197
198    { "nativeStart", "(I)V", (void *)android_media_MediaMuxer_start},
199
200    { "nativeWriteSampleData", "(IILjava/nio/ByteBuffer;IIJI)V",
201        (void *)android_media_MediaMuxer_writeSampleData },
202
203    { "nativeStop", "(I)V", (void *)android_media_MediaMuxer_stop},
204
205    { "nativeSetup", "(Ljava/io/FileDescriptor;I)I",
206        (void *)android_media_MediaMuxer_native_setup },
207
208    { "nativeRelease", "(I)V",
209        (void *)android_media_MediaMuxer_native_release },
210
211};
212
213// This function only registers the native methods, and is called from
214// JNI_OnLoad in android_media_MediaPlayer.cpp
215int register_android_media_MediaMuxer(JNIEnv *env) {
216    int err = AndroidRuntime::registerNativeMethods(env,
217                "android/media/MediaMuxer", gMethods, NELEM(gMethods));
218
219    jclass clazz = env->FindClass("android/media/MediaMuxer");
220    CHECK(clazz != NULL);
221
222    gFields.context = env->GetFieldID(clazz, "mNativeContext", "I");
223    CHECK(gFields.context != NULL);
224
225    jclass byteBufClass = env->FindClass("java/nio/ByteBuffer");
226    CHECK(byteBufClass != NULL);
227
228    gFields.arrayID =
229        env->GetMethodID(byteBufClass, "array", "()[B");
230    CHECK(gFields.arrayID != NULL);
231
232    return err;
233}
234