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 "NativeExtractor"
19 
20 #include <jni.h>
21 #include <fstream>
22 #include <string>
23 #include <sys/stat.h>
24 
25 #include "Extractor.h"
26 
Java_com_android_media_benchmark_library_Native_Extract(JNIEnv * env,jobject thiz,jstring jInputFilePath,jstring jInputFileName,jstring jStatsFile)27 extern "C" JNIEXPORT int32_t JNICALL Java_com_android_media_benchmark_library_Native_Extract(
28         JNIEnv *env, jobject thiz, jstring jInputFilePath, jstring jInputFileName,
29         jstring jStatsFile) {
30     UNUSED(thiz);
31     const char *inputFilePath = env->GetStringUTFChars(jInputFilePath, nullptr);
32     const char *inputFileName = env->GetStringUTFChars(jInputFileName, nullptr);
33     string sFilePath = string(inputFilePath) + string(inputFileName);
34     FILE *inputFp = fopen(sFilePath.c_str(), "rb");
35 
36     // Read file properties
37     struct stat buf;
38     stat(sFilePath.c_str(), &buf);
39     size_t fileSize = buf.st_size;
40     int32_t fd = fileno(inputFp);
41 
42     Extractor *extractObj = new Extractor();
43     int32_t trackCount = extractObj->initExtractor((long) fd, fileSize);
44     if (trackCount <= 0) {
45         ALOGE("initExtractor failed");
46         return -1;
47     }
48 
49     int32_t trackID = 0;
50     const char *mime = nullptr;
51     int32_t status = extractObj->extract(trackID);
52     if (status != AMEDIA_OK) {
53         ALOGE("Extraction failed");
54         return -1;
55     }
56 
57     if (inputFp) {
58         fclose(inputFp);
59         inputFp = nullptr;
60     }
61     status = extractObj->setupTrackFormat(trackID);
62     AMediaFormat *format = extractObj->getFormat();
63     if (!format) {
64         ALOGE("format is null!");
65         return -1;
66     }
67     AMediaFormat_getString(format, AMEDIAFORMAT_KEY_MIME, &mime);
68     if (!mime) {
69         ALOGE("mime is null!");
70         return -1;
71     }
72     extractObj->deInitExtractor();
73     const char *statsFile = env->GetStringUTFChars(jStatsFile, nullptr);
74     extractObj->dumpStatistics(string(inputFileName), string(mime), statsFile);
75     env->ReleaseStringUTFChars(jStatsFile, statsFile);
76     env->ReleaseStringUTFChars(jInputFilePath, inputFilePath);
77     env->ReleaseStringUTFChars(jInputFileName, inputFileName);
78 
79     delete extractObj;
80     return status;
81 }
82