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 #include <dlfcn.h>
17 #include <sys/types.h>
18 #include <media/DataSource.h>
19 #include <datasource/FileSource.h>
20 #include <media/stagefright/MediaExtractor.h>
21 #include <android/IMediaExtractor.h>
22 #include <media/stagefright/DataSourceBase.h>
23 #include <media/stagefright/MetaData.h>
24 #include "../includes/common.h"
25 #define LIBNAME "/system/lib64/extractors/libmp4extractor.so"
26 #define LIBNAME_APEX "/apex/com.android.media/lib64/extractors/libmp4extractor.so"
27 
operator new(size_t size)28 void * operator new(size_t size) {
29     if (size > 64 * 1024 * 1024) {
30         exit (EXIT_VULNERABLE);
31     }
32     return malloc(size);
33 }
34 
35 using namespace android;
36 
main(int argc,char ** argv)37 int main(int argc, char **argv) {
38     (void) argc;
39     (void) argv;
40 #if _64_BIT
41     GetExtractorDef getDef = nullptr;
42     if (argc < 2) {
43         return EXIT_FAILURE;
44     }
45 
46     void *libHandle = dlopen(LIBNAME, RTLD_NOW | RTLD_LOCAL);
47     if (!libHandle) {
48         libHandle = dlopen(LIBNAME_APEX, RTLD_NOW | RTLD_LOCAL);
49         if (!libHandle) {
50             return EXIT_FAILURE;
51         }
52     }
53 
54     getDef = (GetExtractorDef)dlsym(libHandle, "GETEXTRACTORDEF");
55     if (!getDef) {
56         dlclose(libHandle);
57         return EXIT_FAILURE;
58     }
59 
60     sp < DataSource > dataSource = new FileSource(argv[1]);
61     if (dataSource == nullptr) {
62         dlclose(libHandle);
63         return EXIT_FAILURE;
64     }
65 
66     void *meta = nullptr;
67     void* creator = nullptr;
68     FreeMetaFunc freeMeta = nullptr;
69     float confidence;
70     if (getDef().def_version == EXTRACTORDEF_VERSION_NDK_V1) {
71         creator = (void*) getDef().u.v2.sniff(dataSource->wrap(), &confidence,
72                                               &meta, &freeMeta);
73     } else if (getDef().def_version == EXTRACTORDEF_VERSION_NDK_V2) {
74         creator = (void*) getDef().u.v3.sniff(dataSource->wrap(), &confidence,
75                                               &meta, &freeMeta);
76     }
77     if (!creator) {
78         dlclose(libHandle);
79         return EXIT_FAILURE;
80     }
81 
82     CMediaExtractor *ret = ((CreatorFunc) creator)(dataSource->wrap(), meta);
83     if (ret == nullptr) {
84         dlclose(libHandle);
85         return EXIT_FAILURE;
86     }
87 
88     if (meta != nullptr && freeMeta != nullptr) {
89         freeMeta(meta);
90     }
91 
92     MediaExtractorCUnwrapper *mediaExtractorCUnwrapper =
93             new MediaExtractorCUnwrapper(ret);
94     MediaTrack* source = mediaExtractorCUnwrapper->getTrack(0);
95     if (source == nullptr) {
96         dlclose(libHandle);
97         return EXIT_FAILURE;
98     }
99 
100     source->start();
101 
102     dlclose(libHandle);
103 #endif /* _64_BIT */
104     return EXIT_SUCCESS;
105 }
106