1 /*
2 * Copyright (C) 2019 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 "NativeMuxer"
19
20 #include <jni.h>
21 #include <fstream>
22 #include <string>
23 #include <sys/stat.h>
24
25 #include "Muxer.h"
26
27 MUXER_OUTPUT_T getMuxerOutFormat(const char *fmt);
28
Java_com_android_media_benchmark_library_Native_Mux(JNIEnv * env,jobject thiz,jstring jInputFilePath,jstring jInputFileName,jstring jOutputFilePath,jstring jStatsFile,jstring jFormat)29 extern "C" JNIEXPORT int32_t JNICALL Java_com_android_media_benchmark_library_Native_Mux(
30 JNIEnv *env, jobject thiz, jstring jInputFilePath, jstring jInputFileName,
31 jstring jOutputFilePath, jstring jStatsFile, jstring jFormat) {
32 UNUSED(thiz);
33 ALOGV("Mux the samples given by extractor");
34 const char *inputFilePath = env->GetStringUTFChars(jInputFilePath, nullptr);
35 const char *inputFileName = env->GetStringUTFChars(jInputFileName, nullptr);
36 string sInputFile = string(inputFilePath) + string(inputFileName);
37 FILE *inputFp = fopen(sInputFile.c_str(), "rb");
38 if (!inputFp) {
39 ALOGE("Unable to open input file for reading");
40 return -1;
41 }
42
43 const char *fmt = env->GetStringUTFChars(jFormat, nullptr);
44 MUXER_OUTPUT_T outputFormat = getMuxerOutFormat(fmt);
45 if (outputFormat == MUXER_OUTPUT_FORMAT_INVALID) {
46 ALOGE("output format is MUXER_OUTPUT_FORMAT_INVALID");
47 return MUXER_OUTPUT_FORMAT_INVALID;
48 }
49
50 Muxer *muxerObj = new Muxer();
51 Extractor *extractor = muxerObj->getExtractor();
52 if (!extractor) {
53 ALOGE("Extractor creation failed");
54 return -1;
55 }
56
57 // Read file properties
58 struct stat buf;
59 stat(sInputFile.c_str(), &buf);
60 size_t fileSize = buf.st_size;
61 int32_t fd = fileno(inputFp);
62
63 int32_t trackCount = extractor->initExtractor(fd, fileSize);
64 if (trackCount <= 0) {
65 ALOGE("initExtractor failed");
66 return -1;
67 }
68
69 for (int curTrack = 0; curTrack < trackCount; curTrack++) {
70 int32_t status = extractor->setupTrackFormat(curTrack);
71 if (status != 0) {
72 ALOGE("Track Format invalid");
73 return -1;
74 }
75
76 uint8_t *inputBuffer = (uint8_t *) malloc(fileSize);
77 if (!inputBuffer) {
78 ALOGE("Allocation Failed");
79 return -1;
80 }
81 vector<AMediaCodecBufferInfo> frameInfos;
82 AMediaCodecBufferInfo info;
83 uint32_t inputBufferOffset = 0;
84
85 // Get Frame Data
86 while (1) {
87 status = extractor->getFrameSample(info);
88 if (status || !info.size) break;
89 // copy the meta data and buffer to be passed to muxer
90 if (inputBufferOffset + info.size > fileSize) {
91 ALOGE("Memory allocated not sufficient");
92 if (inputBuffer) {
93 free(inputBuffer);
94 inputBuffer = nullptr;
95 }
96 return -1;
97 }
98 memcpy(inputBuffer + inputBufferOffset, extractor->getFrameBuf(),
99 static_cast<size_t>(info.size));
100 info.offset = inputBufferOffset;
101 frameInfos.push_back(info);
102 inputBufferOffset += info.size;
103 }
104
105 const char *outputFilePath = env->GetStringUTFChars(jOutputFilePath, nullptr);
106 FILE *outputFp = fopen(((string) outputFilePath).c_str(), "w+b");
107 env->ReleaseStringUTFChars(jOutputFilePath, outputFilePath);
108
109 if (!outputFp) {
110 ALOGE("Unable to open output file for writing");
111 if (inputBuffer) {
112 free(inputBuffer);
113 inputBuffer = nullptr;
114 }
115 return -1;
116 }
117 int32_t outFd = fileno(outputFp);
118
119 status = muxerObj->initMuxer(outFd, (MUXER_OUTPUT_T) outputFormat);
120 if (status != 0) {
121 ALOGE("initMuxer failed");
122 if (inputBuffer) {
123 free(inputBuffer);
124 inputBuffer = nullptr;
125 }
126 return -1;
127 }
128
129 status = muxerObj->mux(inputBuffer, frameInfos);
130 if (status != 0) {
131 ALOGE("Mux failed");
132 if (inputBuffer) {
133 free(inputBuffer);
134 inputBuffer = nullptr;
135 }
136 return -1;
137 }
138 muxerObj->deInitMuxer();
139 const char *statsFile = env->GetStringUTFChars(jStatsFile, nullptr);
140 string muxFormat(fmt);
141 muxerObj->dumpStatistics(string(inputFileName), muxFormat, statsFile);
142 env->ReleaseStringUTFChars(jStatsFile, statsFile);
143 env->ReleaseStringUTFChars(jInputFilePath, inputFilePath);
144 env->ReleaseStringUTFChars(jInputFileName, inputFileName);
145
146 if (inputBuffer) {
147 free(inputBuffer);
148 inputBuffer = nullptr;
149 }
150 if (outputFp) {
151 fclose(outputFp);
152 outputFp = nullptr;
153 }
154 muxerObj->resetMuxer();
155 }
156 if (inputFp) {
157 fclose(inputFp);
158 inputFp = nullptr;
159 }
160 env->ReleaseStringUTFChars(jFormat, fmt);
161 extractor->deInitExtractor();
162 delete muxerObj;
163
164 return 0;
165 }
166
getMuxerOutFormat(const char * fmt)167 MUXER_OUTPUT_T getMuxerOutFormat(const char *fmt) {
168 static const struct {
169 const char *name;
170 int value;
171 } kFormatMaps[] = {{"mp4", MUXER_OUTPUT_FORMAT_MPEG_4},
172 {"webm", MUXER_OUTPUT_FORMAT_WEBM},
173 {"3gpp", MUXER_OUTPUT_FORMAT_3GPP},
174 {"ogg", MUXER_OUTPUT_FORMAT_OGG}};
175
176 int32_t muxOutputFormat = MUXER_OUTPUT_FORMAT_INVALID;
177 for (auto kFormatMap : kFormatMaps) {
178 if (!strcmp(fmt, kFormatMap.name)) {
179 muxOutputFormat = kFormatMap.value;
180 break;
181 }
182 }
183 return (MUXER_OUTPUT_T) muxOutputFormat;
184 }
185