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