• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2013 Rockchip Electronics 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       Rockchip_OMX_Component_Register.c
20  * @brief      Rockchip OpenMAX IL Component Register
21  * @author     csy (csy@rock-chips.com)
22  * @version    1.0.0
23  * @history
24  *    2013.11.26 : Create
25  */
26 #undef  ROCKCHIP_LOG_TAG
27 #define ROCKCHIP_LOG_TAG    "omx_comp_regs"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <dlfcn.h>
33 #include <sys/types.h>
34 #include <dirent.h>
35 #include <errno.h>
36 #include <assert.h>
37 #include <dirent.h>
38 #include <pthread.h>
39 #include "OMX_Component.h"
40 #include "Rockchip_OSAL_Memory.h"
41 #include "Rockchip_OSAL_ETC.h"
42 #include "Rockchip_OSAL_Library.h"
43 #include "Rockchip_OMX_Macros.h"
44 #include "Rockchip_OMX_Component_Register.h"
45 #include "Rockchip_OSAL_Log.h"
46 #include "Rockchip_OSAL_Mutex.h"
47 
48 static const ROCKCHIP_COMPONENT_INFO kCompInfo[] = {
49     { "rk.omx_dec", "libomxvpu_dec.z.so" },
50     { "rk.omx_enc", "libomxvpu_enc.z.so" },
51 };
52 
53 static ROCKCHIP_OPENED_LIB gOpenedLIb[] = {
54     {"libomxvpu_dec.z.so", NULL },
55     {"libomxvpu_enc.z.so", NULL }
56 };
57 
Rockchip_OMX_Component_Register(ROCKCHIP_OMX_COMPONENT_REGLIST ** compList,OMX_U32 * compNum)58 OMX_ERRORTYPE Rockchip_OMX_Component_Register(ROCKCHIP_OMX_COMPONENT_REGLIST **compList, OMX_U32 *compNum)
59 {
60     OMX_ERRORTYPE  ret = OMX_ErrorNone;
61     int            componentNum = 0, totalCompNum = 0;
62     OMX_U32        i = 0;
63     const char    *errorMsg;
64     omx_err_f("in");
65 
66     int (*Rockchip_OMX_COMPONENT_Library_Register)(RockchipRegisterComponentType **rockchipComponents);
67     RockchipRegisterComponentType **rockchipComponentsTemp;
68     ROCKCHIP_OMX_COMPONENT_REGLIST *componentList;
69 
70     FunctionIn();
71 
72     componentList = (ROCKCHIP_OMX_COMPONENT_REGLIST *)Rockchip_OSAL_Malloc(sizeof(ROCKCHIP_OMX_COMPONENT_REGLIST) *
73         MAX_OMX_COMPONENT_NUM);
74     Rockchip_OSAL_Memset(componentList, 0, sizeof(ROCKCHIP_OMX_COMPONENT_REGLIST) * MAX_OMX_COMPONENT_NUM);
75 
76     for (i = 0; i < ARRAY_SIZE(kCompInfo); i++) {
77         ROCKCHIP_COMPONENT_INFO com_inf = kCompInfo[i];
78         OMX_PTR soHandle = NULL;
79         omx_err_f("in");
80         if ((soHandle = Rockchip_OSAL_dlopen(com_inf.lib_name, RTLD_NOW)) != NULL) {
81             omx_err_f("in so name: %s", com_inf.lib_name);
82             Rockchip_OSAL_dlerror();    /* clear error */
83             if ((Rockchip_OMX_COMPONENT_Library_Register = Rockchip_OSAL_dlsym(soHandle,
84                 "Rockchip_OMX_COMPONENT_Library_Register")) != NULL) {
85                 int i = 0;
86                 unsigned int j = 0;
87                 componentNum = (*Rockchip_OMX_COMPONENT_Library_Register)(NULL);
88                 omx_err_f("in num: %d", componentNum);
89                 rockchipComponentsTemp =
90                     (RockchipRegisterComponentType **)Rockchip_OSAL_Malloc(sizeof(RockchipRegisterComponentType*) *
91                         componentNum);
92                 for (i = 0; i < componentNum; i++) {
93                     rockchipComponentsTemp[i] = Rockchip_OSAL_Malloc(sizeof(RockchipRegisterComponentType));
94                     Rockchip_OSAL_Memset(rockchipComponentsTemp[i], 0, sizeof(RockchipRegisterComponentType));
95                 }
96                 (*Rockchip_OMX_COMPONENT_Library_Register)(rockchipComponentsTemp);
97 
98                 for (i = 0; i < componentNum; i++) {
99                     Rockchip_OSAL_Strcpy(componentList[totalCompNum].component.componentName,
100                         rockchipComponentsTemp[i]->componentName);
101                     for (j = 0; j < rockchipComponentsTemp[i]->totalRoleNum; j++)
102                         Rockchip_OSAL_Strcpy(componentList[totalCompNum].component.roles[j],
103                             rockchipComponentsTemp[i]->roles[j]);
104                     componentList[totalCompNum].component.totalRoleNum = rockchipComponentsTemp[i]->totalRoleNum;
105 
106                     Rockchip_OSAL_Strcpy(componentList[totalCompNum].libName, com_inf.lib_name);
107 
108                     totalCompNum++;
109                 }
110                 for (i = 0; i < componentNum; i++) {
111                     Rockchip_OSAL_Free(rockchipComponentsTemp[i]);
112                 }
113 
114                 Rockchip_OSAL_Free(rockchipComponentsTemp);
115             } else {
116                 if ((errorMsg = Rockchip_OSAL_dlerror()) != NULL)
117                     omx_warn("dlsym failed: %s", errorMsg);
118             }
119             gOpenedLIb[i].libHandle = soHandle;
120         }
121         omx_err_f("Rockchip_OSAL_dlerror: %s", Rockchip_OSAL_dlerror());
122     }
123     *compList = componentList;
124     *compNum = totalCompNum;
125 
126     FunctionOut();
127 
128     return ret;
129 }
130 
Rockchip_OMX_Component_Unregister(ROCKCHIP_OMX_COMPONENT_REGLIST * componentList)131 OMX_ERRORTYPE Rockchip_OMX_Component_Unregister(ROCKCHIP_OMX_COMPONENT_REGLIST *componentList)
132 {
133     OMX_ERRORTYPE ret = OMX_ErrorNone;
134 
135     Rockchip_OSAL_Free(componentList);
136 
137     return ret;
138 }
139 
Rockchip_OMX_ComponentAPICheck(OMX_COMPONENTTYPE * component)140 OMX_ERRORTYPE Rockchip_OMX_ComponentAPICheck(OMX_COMPONENTTYPE *component)
141 {
142     OMX_ERRORTYPE ret = OMX_ErrorNone;
143 
144     if ((component->GetComponentVersion == NULL)    ||
145         (component->SendCommand == NULL)            ||
146         (component->GetParameter == NULL)           ||
147         (component->SetParameter == NULL)           ||
148         (component->GetConfig == NULL)              ||
149         (component->SetConfig == NULL)              ||
150         (component->GetExtensionIndex == NULL)      ||
151         (component->GetState == NULL)               ||
152         (component->ComponentTunnelRequest == NULL) ||
153         (component->UseBuffer == NULL)              ||
154         (component->AllocateBuffer == NULL)         ||
155         (component->FreeBuffer == NULL)             ||
156         (component->EmptyThisBuffer == NULL)        ||
157         (component->FillThisBuffer == NULL)         ||
158         (component->SetCallbacks == NULL)           ||
159         (component->ComponentDeInit == NULL)        ||
160         (component->UseEGLImage == NULL)            ||
161         (component->ComponentRoleEnum == NULL))
162         ret = OMX_ErrorInvalidComponent;
163     else
164         ret = OMX_ErrorNone;
165 
166     return ret;
167 }
168 
Rockchip_OMX_ComponentLoad(ROCKCHIP_OMX_COMPONENT * rockchip_component)169 OMX_ERRORTYPE Rockchip_OMX_ComponentLoad(ROCKCHIP_OMX_COMPONENT *rockchip_component)
170 {
171     OMX_ERRORTYPE      ret = OMX_ErrorNone;
172     OMX_HANDLETYPE     libHandle = NULL;
173     OMX_COMPONENTTYPE *pOMXComponent;
174 
175     FunctionIn();
176     OMX_ERRORTYPE (*Rockchip_OMX_ComponentConstructor)(OMX_HANDLETYPE hComponent, OMX_STRING componentName);
177     for (size_t i = 0; i < ARRAY_SIZE(gOpenedLIb); i++) {
178         if (!strcmp(gOpenedLIb[i].lib_name, (const char*)rockchip_component->libName)) {
179             libHandle = (OMX_HANDLETYPE)gOpenedLIb[i].libHandle;
180             break;
181         }
182     }
183     if (!libHandle) {
184         ret = OMX_ErrorInvalidComponentName;
185         omx_err("OMX_ErrorInvalidComponentName, Line:%d", __LINE__);
186         goto EXIT;
187     }
188 
189     Rockchip_OMX_ComponentConstructor = Rockchip_OSAL_dlsym(libHandle, "Rockchip_OMX_ComponentConstructor");
190     if (!Rockchip_OMX_ComponentConstructor) {
191         ret = OMX_ErrorInvalidComponent;
192         omx_err("OMX_ErrorInvalidComponent, Line:%d", __LINE__);
193         goto EXIT;
194     }
195 
196     pOMXComponent = (OMX_COMPONENTTYPE *)Rockchip_OSAL_Malloc(sizeof(OMX_COMPONENTTYPE));
197     INIT_SET_SIZE_VERSION(pOMXComponent, OMX_COMPONENTTYPE);
198     ret = (*Rockchip_OMX_ComponentConstructor)((OMX_HANDLETYPE)pOMXComponent,
199         (OMX_STRING)rockchip_component->componentName);
200     if (ret != OMX_ErrorNone) {
201         Rockchip_OSAL_Free(pOMXComponent);
202         ret = OMX_ErrorInvalidComponent;
203         omx_err("OMX_ErrorInvalidComponent, Line:%d", __LINE__);
204         goto EXIT;
205     } else {
206         if (Rockchip_OMX_ComponentAPICheck(pOMXComponent) != OMX_ErrorNone) {
207             if (pOMXComponent->ComponentDeInit != NULL)
208                 pOMXComponent->ComponentDeInit(pOMXComponent);
209             Rockchip_OSAL_Free(pOMXComponent);
210             ret = OMX_ErrorInvalidComponent;
211             omx_err("OMX_ErrorInvalidComponent, Line:%d", __LINE__);
212             goto EXIT;
213         }
214         rockchip_component->libHandle = libHandle;
215         rockchip_component->pOMXComponent = pOMXComponent;
216         ret = OMX_ErrorNone;
217     }
218 
219 EXIT:
220     FunctionOut();
221 
222     return ret;
223 }
224 
Rockchip_OMX_ComponentUnload(ROCKCHIP_OMX_COMPONENT * rockchip_component)225 OMX_ERRORTYPE Rockchip_OMX_ComponentUnload(ROCKCHIP_OMX_COMPONENT *rockchip_component)
226 {
227     OMX_ERRORTYPE ret = OMX_ErrorNone;
228     OMX_COMPONENTTYPE *pOMXComponent = NULL;
229 
230     FunctionIn();
231     if (!rockchip_component) {
232         ret = OMX_ErrorBadParameter;
233         goto EXIT;
234     }
235 
236     pOMXComponent = rockchip_component->pOMXComponent;
237     if (pOMXComponent != NULL) {
238         pOMXComponent->ComponentDeInit(pOMXComponent);
239         Rockchip_OSAL_Free(pOMXComponent);
240         rockchip_component->pOMXComponent = NULL;
241     }
242 
243     if (rockchip_component->libHandle != NULL) {
244         rockchip_component->libHandle = NULL;
245     }
246 
247 EXIT:
248     FunctionOut();
249 
250     return ret;
251 }