• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (C) 2021 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 #include <IMediaExtractor.h>
18 #include <dlfcn.h>
19 #include <signal.h>
20 #include <stdlib.h>
21 #include <fcntl.h>
22 
23 #include "../includes/common.h"
24 #include "../includes/memutils.h"
25 
26 #if _32_BIT
27 #define LIBNAME "/system/lib/extractors/libmidiextractor.so"
28 #define LIBNAME_APEX                                                           \
29   "/apex/com.android.media/lib/extractors/libmidiextractor.so"
30 #elif _64_BIT
31 #define LIBNAME "/system/lib64/extractors/libmidiextractor.so"
32 #define LIBNAME_APEX                                                           \
33   "/apex/com.android.media/lib64/extractors/libmidiextractor.so"
34 #endif
35 
36 char enable_selective_overload = ENABLE_NONE;
37 
38 using namespace android;
39 
40 class XMFDataSource : public DataSource {
41 public:
42   int mFdData;
43   int mFdInfo;
XMFDataSource(int fdData,int fdInfo)44   XMFDataSource(int fdData, int fdInfo) {
45     mFdData = fdData;
46     mFdInfo = fdInfo;
47   }
48 
49   ~XMFDataSource() = default;
50 
readAt(off64_t offset,void * data,size_t size)51   virtual ssize_t readAt(off64_t offset __attribute__((unused)), void *data,
52                          size_t size) {
53     uint32_t infoOffset, infoSize;
54     read(mFdInfo, &infoSize, sizeof(int32_t));
55     read(mFdInfo, &infoOffset, sizeof(int32_t));
56     lseek(mFdData, infoOffset, SEEK_SET);
57     read(mFdData, data, infoSize);
58     return size;
59   }
60 
getSize(off64_t * size)61   virtual status_t getSize(off64_t *size) {
62     *size = 0x10000;
63     return 0;
64   }
initCheck() const65   virtual status_t initCheck() const { return 0; }
66 };
67 
close_resources(int fdData,int fdInfo,void * libHandle)68 void close_resources(int fdData, int fdInfo, void *libHandle) {
69   if (fdData >= 0) {
70     ::close(fdData);
71   }
72   if (fdInfo >= 0) {
73     ::close(fdInfo);
74   }
75   if (libHandle) {
76     dlclose(libHandle);
77   }
78 }
79 
main(int argc,char ** argv)80 int main(int argc, char **argv) {
81   if (argc < 3) {
82     return EXIT_FAILURE;
83   }
84   enable_selective_overload = ENABLE_ALL;
85   void *libHandle = dlopen(LIBNAME, RTLD_NOW | RTLD_LOCAL);
86   if (!libHandle) {
87     libHandle = dlopen(LIBNAME_APEX, RTLD_NOW | RTLD_LOCAL);
88     if (!libHandle) {
89       return EXIT_FAILURE;
90     }
91   }
92 
93   GetExtractorDef getDef = (GetExtractorDef)dlsym(libHandle, "GETEXTRACTORDEF");
94   if (!getDef) {
95     dlclose(libHandle);
96     return EXIT_FAILURE;
97   }
98 
99   int fdData = open(argv[1], O_RDONLY);
100   if (fdData < 0) {
101     dlclose(libHandle);
102     return EXIT_FAILURE;
103   }
104   int fdInfo = open(argv[2], O_RDONLY);
105   if (fdInfo < 0) {
106     close_resources(fdData, fdInfo, libHandle);
107     return EXIT_FAILURE;
108   }
109 
110   sp<DataSource> dataSource = (sp<DataSource>)new XMFDataSource(fdData, fdInfo);
111   if (!dataSource) {
112     close_resources(fdData, fdInfo, libHandle);
113     return EXIT_FAILURE;
114   }
115 
116   void *meta = nullptr;
117   FreeMetaFunc freeMeta = nullptr;
118 
119   float confidence = 0.0f;
120   if (getDef().def_version == EXTRACTORDEF_VERSION_NDK_V1) {
121     getDef().u.v2.sniff(dataSource->wrap(), &confidence, &meta, &freeMeta);
122   } else if (getDef().def_version == EXTRACTORDEF_VERSION_NDK_V2) {
123     getDef().u.v3.sniff(dataSource->wrap(), &confidence, &meta, &freeMeta);
124   }
125 
126   close_resources(fdData, fdInfo, libHandle);
127   enable_selective_overload = ENABLE_NONE;
128   return EXIT_SUCCESS;
129 }
130