blob: d4348e1879f64f91cec404adbb9cd4aa9e921e1f
1 | /* |
2 | * Copyright (C) 2008 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 <ammodule.h> |
18 | #include <dlfcn.h> |
19 | #include <string.h> |
20 | #include <pthread.h> |
21 | #include <errno.h> |
22 | #include <limits.h> |
23 | #include <unistd.h> |
24 | #define LOG_TAG "ammodule" |
25 | #ifdef ANDROID |
26 | #include <utils/Log.h> |
27 | #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) |
28 | #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) |
29 | #define LOGV(...) |
30 | #else |
31 | #define LOGI printf |
32 | #define LOGE printf |
33 | #define LOGV printf |
34 | #endif |
35 | #include <amconfigutils.h> |
36 | /** Base path of the hal modules */ |
37 | #define AM_LIBRARY_PATH1 "/system/lib/amplayer" |
38 | #define AM_LIBRARY_PATH2 "/vendor/lib/amplayer" |
39 | #define AM_LIBRARY_SETTING "media.libplayer.modulepath" |
40 | |
41 | static const char *defaut_path[] = { |
42 | AM_LIBRARY_PATH1, |
43 | AM_LIBRARY_PATH2, |
44 | ""/*real path.*/ |
45 | |
46 | }; |
47 | |
48 | static const int PATH_COUNT = |
49 | (sizeof(defaut_path) / sizeof(defaut_path[0])); |
50 | |
51 | /** |
52 | * Load the file defined by the variant and if successful |
53 | * return the dlopen handle and the hmi. |
54 | * @return 0 = success, !0 = failure. |
55 | */ |
56 | static int amload(const char *path, |
57 | const struct ammodule_t **pHmi) |
58 | { |
59 | int status; |
60 | void *handle; |
61 | struct ammodule_t *hmi; |
62 | |
63 | /* |
64 | * load the symbols resolving undefined symbols before |
65 | * dlopen returns. Since RTLD_GLOBAL is not or'd in with |
66 | * RTLD_NOW the external symbols will not be global |
67 | */ |
68 | handle = dlopen(path, RTLD_NOW); |
69 | if (handle == NULL) { |
70 | char const *err_str = dlerror(); |
71 | LOGE("amload: module=%s\n%s", path, err_str ? err_str : "unknown"); |
72 | status = -EINVAL; |
73 | goto done; |
74 | } |
75 | |
76 | /* Get the address of the struct hal_module_info. */ |
77 | const char *sym = AMPLAYER_MODULE_INFO_SYM_AS_STR; |
78 | hmi = (struct ammodule_t *)dlsym(handle, sym); |
79 | if (hmi == NULL) { |
80 | LOGE("amload: couldn't find symbol %s", sym); |
81 | status = -EINVAL; |
82 | goto done; |
83 | } |
84 | |
85 | hmi->dso = handle; |
86 | |
87 | /* success */ |
88 | status = 0; |
89 | if (hmi->tag != AMPLAYER_MODULE_TAG || |
90 | hmi->version_major != AMPLAYER_API_MAIOR) { |
91 | status = -1; |
92 | LOGE("module tag,api unsupport tag=%d,expect=%d api=%d.%d,expect=%d.%d\n", |
93 | hmi->tag, AMPLAYER_MODULE_TAG, |
94 | hmi->version_major, hmi->version_minor, |
95 | AMPLAYER_API_MAIOR, AMPLAYER_API_MINOR); |
96 | } |
97 | done: |
98 | if (status != 0) { |
99 | hmi = NULL; |
100 | if (handle != NULL) { |
101 | dlclose(handle); |
102 | handle = NULL; |
103 | } |
104 | } else { |
105 | LOGV("loaded module path=%s hmi=%p handle=%p", |
106 | path, *pHmi, handle); |
107 | } |
108 | |
109 | *pHmi = hmi; |
110 | |
111 | return status; |
112 | } |
113 | |
114 | int ammodule_load_module(const char *modulename, const struct ammodule_t **module) |
115 | { |
116 | int status = -ENOENT;; |
117 | int i; |
118 | const struct ammodule_t *hmi = NULL; |
119 | char prop[PATH_MAX]; |
120 | char path[PATH_MAX]; |
121 | char name[PATH_MAX]; |
122 | const char *prepath = NULL; |
123 | |
124 | snprintf(name, PATH_MAX, "%s", modulename); |
125 | |
126 | for (i = -1 ; i < PATH_COUNT; i++) { |
127 | if (i >= 0) { |
128 | prepath = defaut_path[i]; |
129 | } else { |
130 | if (am_getconfig(AM_LIBRARY_SETTING, prop, NULL) <= 0) { |
131 | continue; |
132 | } |
133 | prepath = prop; |
134 | } |
135 | snprintf(path, sizeof(path), "%s/lib%s.so", |
136 | prepath, name); |
137 | if (access(path, R_OK) == 0) { |
138 | break; |
139 | } |
140 | snprintf(path, sizeof(path), "%s/%s.so", |
141 | prepath, name); |
142 | if (access(path, R_OK) == 0) { |
143 | break; |
144 | } |
145 | |
146 | snprintf(path, sizeof(path), "%s/%s", |
147 | prepath, name); |
148 | if (access(path, R_OK) == 0) { |
149 | break; |
150 | } |
151 | snprintf(path, sizeof(path), "%s", |
152 | name); |
153 | if (access(path, R_OK) == 0) { |
154 | break; |
155 | } |
156 | } |
157 | |
158 | status = -ENOENT; |
159 | if (i < PATH_COUNT) { |
160 | /* load the module, if this fails, we're doomed, and we should not try |
161 | * to load a different variant. */ |
162 | status = amload(path, module); |
163 | } |
164 | LOGI("load mode %s,on %s %d\n", modulename, path, status); |
165 | return status; |
166 | } |
167 | |
168 | int ammodule_open_module(struct ammodule_t *module) |
169 | { |
170 | int ret = -1000; |
171 | |
172 | if (module->methods) { |
173 | ret = module->methods->init(module, 0); |
174 | } |
175 | if (ret != 0) { |
176 | LOGE("open module (%s) failed ret(%d)\n", module->name, ret); |
177 | } else { |
178 | LOGI("open module success,\n\tname:%s\n\t%s\n", module->name, module->descript); |
179 | } |
180 | return 0; |
181 | } |
182 | int ammodule_match_check(const char* filefmtstr, const char* fmtsetting) |
183 | { |
184 | const char * psets = fmtsetting; |
185 | const char *psetend; |
186 | int psetlen = 0; |
187 | char codecstr[64] = ""; |
188 | if (filefmtstr == NULL || fmtsetting == NULL) { |
189 | return 0; |
190 | } |
191 | |
192 | while (psets && psets[0] != '\0') { |
193 | psetlen = 0; |
194 | psetend = strchr(psets, ','); |
195 | if (psetend != NULL && psetend > psets && psetend - psets < 64) { |
196 | psetlen = psetend - psets; |
197 | memcpy(codecstr, psets, psetlen); |
198 | codecstr[psetlen] = '\0'; |
199 | psets = &psetend[1]; //skip ";" |
200 | } else { |
201 | strcpy(codecstr, psets); |
202 | psets = NULL; |
203 | } |
204 | if (strlen(codecstr) > 0) { |
205 | if (strstr(filefmtstr, codecstr) != NULL) { |
206 | return 1; |
207 | } |
208 | } |
209 | } |
210 | return 0; |
211 | } |
212 | |
213 | int ammodule_simple_load_module(char* name) |
214 | { |
215 | int ret; |
216 | struct ammodule_t *module; |
217 | ret = ammodule_load_module(name, &module); |
218 | if (ret == 0) { |
219 | ret = ammodule_open_module(module); |
220 | } |
221 | return ret; |
222 | |
223 | } |