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