blob: b664325ab0d1444e336989ae95b639e9a6e10109 [file] [log] [blame]
ztenghui68ccf102013-02-13 14:07:02 -08001/*
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 Hancb1e8962019-02-21 14:18:11 +090021#include "android_media_Streams.h"
ztenghui68ccf102013-02-13 14:07:02 -080022#include "android_runtime/AndroidRuntime.h"
23#include "jni.h"
Orion Hodson864d3042020-06-02 13:22:06 +010024#include <nativehelper/JNIPlatformHelp.h>
ztenghui68ccf102013-02-13 14:07:02 -080025
Hangyu Kuang61c74c6a2017-01-20 10:15:04 -080026#include <unistd.h>
27#include <fcntl.h>
28
ztenghui68ccf102013-02-13 14:07:02 -080029#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
34namespace android {
35
36struct fields_t {
ztenghui68ccf102013-02-13 14:07:02 -080037 jmethodID arrayID;
38};
39
40static fields_t gFields;
41
42}
43
44using namespace android;
45
46static jint android_media_MediaMuxer_addTrack(
Andreas Gampe5a15d0d2014-11-10 18:19:40 -080047 JNIEnv *env, jclass /* clazz */, jlong nativeObject, jobjectArray keys,
ztenghui68ccf102013-02-13 14:07:02 -080048 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
76static void android_media_MediaMuxer_writeSampleData(
Andreas Gampe5a15d0d2014-11-10 18:19:40 -080077 JNIEnv *env, jclass /* clazz */, jlong nativeObject, jint trackIndex,
ztenghui68ccf102013-02-13 14:07:02 -080078 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änzer46c82b42014-11-30 11:04:10 +0100113 (long long)dstSize, size, offset);
ztenghui68ccf102013-02-13 14:07:02 -0800114 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 Bhatd2507a22014-02-07 12:22:50 +0000138static jlong android_media_MediaMuxer_native_setup(
ztenghui68ccf102013-02-13 14:07:02 -0800139 JNIEnv *env, jclass clazz, jobject fileDescriptor,
140 jint format) {
141 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
142 ALOGV("native_setup: fd %d", fd);
143
Hangyu Kuang61c74c6a2017-01-20 10:15:04 -0800144 // 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
ztenghui68ccf102013-02-13 14:07:02 -0800166 MediaMuxer::OutputFormat fileFormat =
167 static_cast<MediaMuxer::OutputFormat>(format);
Manisha Jajoo29eda3f2021-10-09 01:07:51 +0530168 sp<MediaMuxer> muxer = MediaMuxer::create(fd, fileFormat);
169 if (muxer == nullptr) {
170 jniThrowException(env, "java/lang/IllegalArgumentException", "Muxer creation failed");
171 return 0;
172 }
ztenghui68ccf102013-02-13 14:07:02 -0800173 muxer->incStrong(clazz);
Ashok Bhatd2507a22014-02-07 12:22:50 +0000174 return reinterpret_cast<jlong>(muxer.get());
ztenghui68ccf102013-02-13 14:07:02 -0800175}
176
ztenghuieffc9b42013-03-12 16:03:12 -0700177static void android_media_MediaMuxer_setOrientationHint(
Andreas Gampe5a15d0d2014-11-10 18:19:40 -0800178 JNIEnv *env, jclass /* clazz */, jlong nativeObject, jint degrees) {
ztenghuieffc9b42013-03-12 16:03:12 -0700179 sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject));
180 if (muxer == NULL) {
181 jniThrowException(env, "java/lang/IllegalStateException",
182 "Muxer was not set up correctly");
183 return;
184 }
185 status_t err = muxer->setOrientationHint(degrees);
186
187 if (err != OK) {
188 jniThrowException(env, "java/lang/IllegalStateException",
189 "Failed to set orientation hint");
190 return;
191 }
192
193}
194
Zhijun Hecfd47482013-09-09 15:47:06 -0700195static void android_media_MediaMuxer_setLocation(
Andreas Gampe5a15d0d2014-11-10 18:19:40 -0800196 JNIEnv *env, jclass /* clazz */, jlong nativeObject, jint latitude, jint longitude) {
Zhijun Hecfd47482013-09-09 15:47:06 -0700197 MediaMuxer* muxer = reinterpret_cast<MediaMuxer *>(nativeObject);
198
199 status_t res = muxer->setLocation(latitude, longitude);
200 if (res != OK) {
201 jniThrowException(env, "java/lang/IllegalStateException",
202 "Failed to set location");
203 return;
204 }
205}
206
Andreas Gampe5a15d0d2014-11-10 18:19:40 -0800207static void android_media_MediaMuxer_start(JNIEnv *env, jclass /* clazz */,
Ashok Bhat656fd042013-11-28 10:56:06 +0000208 jlong nativeObject) {
ztenghui68ccf102013-02-13 14:07:02 -0800209 sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject));
210 if (muxer == NULL) {
211 jniThrowException(env, "java/lang/IllegalStateException",
212 "Muxer was not set up correctly");
213 return;
214 }
215 status_t err = muxer->start();
216
217 if (err != OK) {
218 jniThrowException(env, "java/lang/IllegalStateException",
219 "Failed to start the muxer");
220 return;
221 }
222
223}
224
Andreas Gampe5a15d0d2014-11-10 18:19:40 -0800225static void android_media_MediaMuxer_stop(JNIEnv *env, jclass /* clazz */,
Ashok Bhat656fd042013-11-28 10:56:06 +0000226 jlong nativeObject) {
ztenghui68ccf102013-02-13 14:07:02 -0800227 sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject));
228 if (muxer == NULL) {
229 jniThrowException(env, "java/lang/IllegalStateException",
230 "Muxer was not set up correctly");
231 return;
232 }
233
234 status_t err = muxer->stop();
235
Gopalakrishnan Nallasamy123e60e2020-05-13 00:24:15 -0700236 if (err != OK) {
237 ALOGE("Error during stop:%d", err);
238 jniThrowException(env, "java/lang/IllegalStateException",
239 "Error during stop(), muxer would have stopped already");
240 return;
ztenghui68ccf102013-02-13 14:07:02 -0800241 }
242}
243
244static void android_media_MediaMuxer_native_release(
Andreas Gampe5a15d0d2014-11-10 18:19:40 -0800245 JNIEnv* /* env */, jclass clazz, jlong nativeObject) {
ztenghui68ccf102013-02-13 14:07:02 -0800246 sp<MediaMuxer> muxer(reinterpret_cast<MediaMuxer *>(nativeObject));
247 if (muxer != NULL) {
248 muxer->decStrong(clazz);
249 }
250}
251
Daniel Micay76f6a862015-09-19 17:31:01 -0400252static const JNINativeMethod gMethods[] = {
ztenghui68ccf102013-02-13 14:07:02 -0800253
Ashok Bhat656fd042013-11-28 10:56:06 +0000254 { "nativeAddTrack", "(J[Ljava/lang/String;[Ljava/lang/Object;)I",
ztenghui68ccf102013-02-13 14:07:02 -0800255 (void *)android_media_MediaMuxer_addTrack },
256
Ashok Bhat656fd042013-11-28 10:56:06 +0000257 { "nativeSetOrientationHint", "(JI)V",
ztenghuieffc9b42013-03-12 16:03:12 -0700258 (void *)android_media_MediaMuxer_setOrientationHint},
259
Ashok Bhat656fd042013-11-28 10:56:06 +0000260 { "nativeSetLocation", "(JII)V",
Zhijun Hecfd47482013-09-09 15:47:06 -0700261 (void *)android_media_MediaMuxer_setLocation},
262
Ashok Bhat656fd042013-11-28 10:56:06 +0000263 { "nativeStart", "(J)V", (void *)android_media_MediaMuxer_start},
ztenghui68ccf102013-02-13 14:07:02 -0800264
Ashok Bhat656fd042013-11-28 10:56:06 +0000265 { "nativeWriteSampleData", "(JILjava/nio/ByteBuffer;IIJI)V",
ztenghui68ccf102013-02-13 14:07:02 -0800266 (void *)android_media_MediaMuxer_writeSampleData },
267
Ashok Bhat656fd042013-11-28 10:56:06 +0000268 { "nativeStop", "(J)V", (void *)android_media_MediaMuxer_stop},
ztenghui68ccf102013-02-13 14:07:02 -0800269
Ashok Bhat656fd042013-11-28 10:56:06 +0000270 { "nativeSetup", "(Ljava/io/FileDescriptor;I)J",
ztenghui68ccf102013-02-13 14:07:02 -0800271 (void *)android_media_MediaMuxer_native_setup },
272
Ashok Bhat656fd042013-11-28 10:56:06 +0000273 { "nativeRelease", "(J)V",
ztenghui68ccf102013-02-13 14:07:02 -0800274 (void *)android_media_MediaMuxer_native_release },
275
276};
277
278// This function only registers the native methods, and is called from
279// JNI_OnLoad in android_media_MediaPlayer.cpp
280int register_android_media_MediaMuxer(JNIEnv *env) {
281 int err = AndroidRuntime::registerNativeMethods(env,
282 "android/media/MediaMuxer", gMethods, NELEM(gMethods));
283
ztenghui68ccf102013-02-13 14:07:02 -0800284 jclass byteBufClass = env->FindClass("java/nio/ByteBuffer");
285 CHECK(byteBufClass != NULL);
286
287 gFields.arrayID =
288 env->GetMethodID(byteBufClass, "array", "()[B");
289 CHECK(gFields.arrayID != NULL);
290
291 return err;
292}