Initial version of libomxil-e3250-v4l2
[platform/adaptation/ap_samsung/libomxil-e3250-v4l2.git] / openmax / core / Exynos_OMX_Component_Register.c
1 /*
2  *
3  * Copyright 2012 Samsung Electronics S.LSI Co. LTD
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 /*
19  * @file       Exynos_OMX_Component_Register.c
20  * @brief      Exynos OpenMAX IL Component Register
21  * @author     SeungBeom Kim (sbcrux.kim@samsung.com)
22  * @version    2.0.0
23  * @history
24  *    2012.02.20 : Create
25  */
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <dlfcn.h>
30 #include <sys/types.h>
31 #include <dirent.h>
32 #include <errno.h>
33 #include <assert.h>
34 #include <dirent.h>
35
36 #include "OMX_Component.h"
37 #include "Exynos_OSAL_Memory.h"
38 #include "Exynos_OSAL_ETC.h"
39 #include "Exynos_OSAL_Library.h"
40 #include "Exynos_OMX_Component_Register.h"
41 #include "Exynos_OMX_Macros.h"
42
43 #undef  EXYNOS_LOG_TAG
44 #define EXYNOS_LOG_TAG    "EXYNOS_COMP_REGS"
45 #define EXYNOS_LOG_OFF
46 #include "Exynos_OSAL_Log.h"
47
48 OMX_ERRORTYPE Exynos_OMX_Component_Register(EXYNOS_OMX_COMPONENT_REGLIST **compList, OMX_U32 *compNum)
49 {
50     OMX_ERRORTYPE  ret = OMX_ErrorNone;
51     int            componentNum = 0, roleNum = 0, totalCompNum = 0;
52     int            read;
53     char          *libName;
54     size_t         len;
55     const char    *errorMsg;
56     DIR           *dir;
57     struct dirent *d;
58
59     int (*Exynos_OMX_COMPONENT_Library_Register)(ExynosRegisterComponentType **exynosComponents);
60     ExynosRegisterComponentType **exynosComponentsTemp;
61     EXYNOS_OMX_COMPONENT_REGLIST *componentList;
62
63     FunctionIn();
64
65     dir = opendir(EXYNOS_OMX_INSTALL_PATH);
66     if (dir == NULL) {
67         ret = OMX_ErrorUndefined;
68         goto EXIT;
69     }
70
71     componentList = (EXYNOS_OMX_COMPONENT_REGLIST *)Exynos_OSAL_Malloc(sizeof(EXYNOS_OMX_COMPONENT_REGLIST) * MAX_OMX_COMPONENT_NUM);
72     Exynos_OSAL_Memset(componentList, 0, sizeof(EXYNOS_OMX_COMPONENT_REGLIST) * MAX_OMX_COMPONENT_NUM);
73     libName = Exynos_OSAL_Malloc(MAX_OMX_COMPONENT_LIBNAME_SIZE);
74
75     while ((d = readdir(dir)) != NULL) {
76         OMX_HANDLETYPE soHandle;
77         Exynos_OSAL_Log(EXYNOS_LOG_ERROR, "%s", d->d_name);
78
79         if (Exynos_OSAL_Strncmp(d->d_name, "libOMX.Exynos.", Exynos_OSAL_Strlen("libOMX.Exynos.")) == 0) {
80             Exynos_OSAL_Memset(libName, 0, MAX_OMX_COMPONENT_LIBNAME_SIZE);
81             Exynos_OSAL_Strcpy(libName, EXYNOS_OMX_INSTALL_PATH);
82             Exynos_OSAL_Strcat(libName, d->d_name);
83             Exynos_OSAL_Log(EXYNOS_LOG_ERROR, "Path & libName : %s", libName);
84             if ((soHandle = Exynos_OSAL_dlopen(libName, RTLD_LAZY)) != NULL) {
85                 Exynos_OSAL_dlerror();    /* clear error*/
86                 if ((Exynos_OMX_COMPONENT_Library_Register = Exynos_OSAL_dlsym(soHandle, "Exynos_OMX_COMPONENT_Library_Register")) != NULL) {
87                     int i = 0;
88                     unsigned int j = 0;
89
90                     componentNum = (*Exynos_OMX_COMPONENT_Library_Register)(NULL);
91                     exynosComponentsTemp = (ExynosRegisterComponentType **)Exynos_OSAL_Malloc(sizeof(ExynosRegisterComponentType*) * componentNum);
92                     for (i = 0; i < componentNum; i++) {
93                         exynosComponentsTemp[i] = Exynos_OSAL_Malloc(sizeof(ExynosRegisterComponentType));
94                         Exynos_OSAL_Memset(exynosComponentsTemp[i], 0, sizeof(ExynosRegisterComponentType));
95                     }
96                     (*Exynos_OMX_COMPONENT_Library_Register)(exynosComponentsTemp);
97
98                     for (i = 0; i < componentNum; i++) {
99                         Exynos_OSAL_Strcpy(componentList[totalCompNum].component.componentName, exynosComponentsTemp[i]->componentName);
100                         for (j = 0; j < exynosComponentsTemp[i]->totalRoleNum; j++)
101                             Exynos_OSAL_Strcpy(componentList[totalCompNum].component.roles[j], exynosComponentsTemp[i]->roles[j]);
102                         componentList[totalCompNum].component.totalRoleNum = exynosComponentsTemp[i]->totalRoleNum;
103
104                         Exynos_OSAL_Strcpy(componentList[totalCompNum].libName, libName);
105
106                         totalCompNum++;
107                     }
108                     for (i = 0; i < componentNum; i++) {
109                         Exynos_OSAL_Free(exynosComponentsTemp[i]);
110                     }
111
112                     Exynos_OSAL_Free(exynosComponentsTemp);
113                 } else {
114                     if ((errorMsg = Exynos_OSAL_dlerror()) != NULL)
115                         Exynos_OSAL_Log(EXYNOS_LOG_WARNING, "dlsym failed: %s", errorMsg);
116                 }
117                 Exynos_OSAL_dlclose(soHandle);
118             } else {
119                 Exynos_OSAL_Log(EXYNOS_LOG_WARNING, "dlopen failed: %s", Exynos_OSAL_dlerror());
120             }
121         } else {
122             /* not a component name line. skip */
123             continue;
124         }
125     }
126
127     Exynos_OSAL_Free(libName);
128
129     closedir(dir);
130
131     *compList = componentList;
132     *compNum = totalCompNum;
133
134 EXIT:
135     FunctionOut();
136
137     return ret;
138 }
139
140 OMX_ERRORTYPE Exynos_OMX_Component_Unregister(EXYNOS_OMX_COMPONENT_REGLIST *componentList)
141 {
142     OMX_ERRORTYPE ret = OMX_ErrorNone;
143
144     Exynos_OSAL_Memset(componentList, 0, sizeof(EXYNOS_OMX_COMPONENT_REGLIST) * MAX_OMX_COMPONENT_NUM);
145     Exynos_OSAL_Free(componentList);
146
147 EXIT:
148     return ret;
149 }
150
151 OMX_ERRORTYPE Exynos_OMX_ComponentAPICheck(OMX_COMPONENTTYPE *component)
152 {
153     OMX_ERRORTYPE ret = OMX_ErrorNone;
154
155     if ((NULL == component->GetComponentVersion)    ||
156         (NULL == component->SendCommand)            ||
157         (NULL == component->GetParameter)           ||
158         (NULL == component->SetParameter)           ||
159         (NULL == component->GetConfig)              ||
160         (NULL == component->SetConfig)              ||
161         (NULL == component->GetExtensionIndex)      ||
162         (NULL == component->GetState)               ||
163         (NULL == component->ComponentTunnelRequest) ||
164         (NULL == component->UseBuffer)              ||
165         (NULL == component->AllocateBuffer)         ||
166         (NULL == component->FreeBuffer)             ||
167         (NULL == component->EmptyThisBuffer)        ||
168         (NULL == component->FillThisBuffer)         ||
169         (NULL == component->SetCallbacks)           ||
170         (NULL == component->ComponentDeInit)        ||
171         (NULL == component->UseEGLImage)            ||
172         (NULL == component->ComponentRoleEnum))
173         ret = OMX_ErrorInvalidComponent;
174     else
175         ret = OMX_ErrorNone;
176
177     return ret;
178 }
179
180 OMX_ERRORTYPE Exynos_OMX_ComponentLoad(EXYNOS_OMX_COMPONENT *exynos_component)
181 {
182     OMX_ERRORTYPE      ret = OMX_ErrorNone;
183     OMX_HANDLETYPE     libHandle;
184     OMX_COMPONENTTYPE *pOMXComponent;
185
186     FunctionIn();
187
188     OMX_ERRORTYPE (*Exynos_OMX_ComponentInit)(OMX_HANDLETYPE hComponent, OMX_STRING componentName);
189
190     libHandle = Exynos_OSAL_dlopen((OMX_STRING)exynos_component->libName, RTLD_LAZY);
191     if (!libHandle) {
192         ret = OMX_ErrorInvalidComponentName;
193         Exynos_OSAL_Log(EXYNOS_LOG_ERROR, "OMX_ErrorInvalidComponentName, Line:%d", __LINE__);
194         goto EXIT;
195     }
196
197     Exynos_OMX_ComponentInit = Exynos_OSAL_dlsym(libHandle, "Exynos_OMX_ComponentInit");
198     if (!Exynos_OMX_ComponentInit) {
199         Exynos_OSAL_dlclose(libHandle);
200         ret = OMX_ErrorInvalidComponent;
201         Exynos_OSAL_Log(EXYNOS_LOG_ERROR, "OMX_ErrorInvalidComponent, Line:%d", __LINE__);
202         goto EXIT;
203     }
204
205     pOMXComponent = (OMX_COMPONENTTYPE *)Exynos_OSAL_Malloc(sizeof(OMX_COMPONENTTYPE));
206     INIT_SET_SIZE_VERSION(pOMXComponent, OMX_COMPONENTTYPE);
207     ret = (*Exynos_OMX_ComponentInit)((OMX_HANDLETYPE)pOMXComponent, (OMX_STRING)exynos_component->componentName);
208     if (ret != OMX_ErrorNone) {
209         Exynos_OSAL_Free(pOMXComponent);
210         Exynos_OSAL_dlclose(libHandle);
211         ret = OMX_ErrorInvalidComponent;
212         Exynos_OSAL_Log(EXYNOS_LOG_ERROR, "OMX_ErrorInvalidComponent, Line:%d", __LINE__);
213         goto EXIT;
214     } else {
215         if (Exynos_OMX_ComponentAPICheck(pOMXComponent) != OMX_ErrorNone) {
216             if (NULL != pOMXComponent->ComponentDeInit)
217                 pOMXComponent->ComponentDeInit(pOMXComponent);
218             Exynos_OSAL_Free(pOMXComponent);
219             Exynos_OSAL_dlclose(libHandle);
220             ret = OMX_ErrorInvalidComponent;
221             Exynos_OSAL_Log(EXYNOS_LOG_ERROR, "OMX_ErrorInvalidComponent, Line:%d", __LINE__);
222             goto EXIT;
223         }
224         exynos_component->libHandle = libHandle;
225         exynos_component->pOMXComponent = pOMXComponent;
226         ret = OMX_ErrorNone;
227     }
228
229 EXIT:
230     FunctionOut();
231
232     return ret;
233 }
234
235 OMX_ERRORTYPE Exynos_OMX_ComponentUnload(EXYNOS_OMX_COMPONENT *exynos_component)
236 {
237     OMX_ERRORTYPE ret = OMX_ErrorNone;
238     OMX_COMPONENTTYPE *pOMXComponent = NULL;
239
240     FunctionIn();
241
242     if (!exynos_component) {
243         ret = OMX_ErrorBadParameter;
244         goto EXIT;
245     }
246
247     pOMXComponent = exynos_component->pOMXComponent;
248     if (pOMXComponent != NULL) {
249         pOMXComponent->ComponentDeInit(pOMXComponent);
250         Exynos_OSAL_Free(pOMXComponent);
251         exynos_component->pOMXComponent = NULL;
252     }
253
254     if (exynos_component->libHandle != NULL) {
255         Exynos_OSAL_dlclose(exynos_component->libHandle);
256         exynos_component->libHandle = NULL;
257     }
258
259 EXIT:
260     FunctionOut();
261
262     return ret;
263 }
264