1/*
2 * Copyright (C) 2009 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 "TIOMXPlugin.h"
18
19#include <dlfcn.h>
20
21#include <HardwareAPI.h>
22
23namespace android {
24
25extern "C" OMXPluginBase *createOMXPlugin() {
26    return new TIOMXPlugin;
27}
28
29extern "C" void destroyOMXPlugin(OMXPluginBase *plugin) {
30    delete plugin;
31}
32
33#define LIBOMX "libOMX_Core.so"
34
35TIOMXPlugin::TIOMXPlugin()
36    : mLibHandle(dlopen(LIBOMX, RTLD_NOW)),
37      mInit(NULL),
38      mDeinit(NULL),
39      mComponentNameEnum(NULL),
40      mGetHandle(NULL),
41      mFreeHandle(NULL),
42      mGetRolesOfComponentHandle(NULL) {
43    if (mLibHandle != NULL) {
44        mInit = (InitFunc)dlsym(mLibHandle, "TIOMX_Init");
45        mDeinit = (DeinitFunc)dlsym(mLibHandle, "TIOMX_Deinit");
46
47        mComponentNameEnum =
48            (ComponentNameEnumFunc)dlsym(mLibHandle, "TIOMX_ComponentNameEnum");
49
50        mGetHandle = (GetHandleFunc)dlsym(mLibHandle, "TIOMX_GetHandle");
51        mFreeHandle = (FreeHandleFunc)dlsym(mLibHandle, "TIOMX_FreeHandle");
52
53        mGetRolesOfComponentHandle =
54            (GetRolesOfComponentFunc)dlsym(
55                    mLibHandle, "TIOMX_GetRolesOfComponent");
56
57        (*mInit)();
58    }
59    else
60        ALOGE("%s: failed to load %s", __func__, LIBOMX);
61}
62
63TIOMXPlugin::~TIOMXPlugin() {
64    if (mLibHandle != NULL) {
65        (*mDeinit)();
66
67        dlclose(mLibHandle);
68        mLibHandle = NULL;
69    }
70}
71
72OMX_ERRORTYPE TIOMXPlugin::makeComponentInstance(
73        const char *name,
74        const OMX_CALLBACKTYPE *callbacks,
75        OMX_PTR appData,
76        OMX_COMPONENTTYPE **component) {
77    if (mLibHandle == NULL) {
78        return OMX_ErrorUndefined;
79    }
80
81    return (*mGetHandle)(
82            reinterpret_cast<OMX_HANDLETYPE *>(component),
83            const_cast<char *>(name),
84            appData, const_cast<OMX_CALLBACKTYPE *>(callbacks));
85}
86
87OMX_ERRORTYPE TIOMXPlugin::destroyComponentInstance(
88        OMX_COMPONENTTYPE *component) {
89    if (mLibHandle == NULL) {
90        return OMX_ErrorUndefined;
91    }
92
93    return (*mFreeHandle)(reinterpret_cast<OMX_HANDLETYPE *>(component));
94}
95
96OMX_ERRORTYPE TIOMXPlugin::enumerateComponents(
97        OMX_STRING name,
98        size_t size,
99        OMX_U32 index) {
100    if (mLibHandle == NULL) {
101	ALOGE("mLibHandle is NULL!");
102        return OMX_ErrorUndefined;
103    }
104
105    return (*mComponentNameEnum)(name, size, index);
106}
107
108OMX_ERRORTYPE TIOMXPlugin::getRolesOfComponent(
109        const char *name,
110        Vector<String8> *roles) {
111    roles->clear();
112
113    if (mLibHandle == NULL) {
114        return OMX_ErrorUndefined;
115    }
116
117    OMX_U32 numRoles;
118    OMX_ERRORTYPE err = (*mGetRolesOfComponentHandle)(
119            const_cast<OMX_STRING>(name), &numRoles, NULL);
120
121    if (err != OMX_ErrorNone) {
122        return err;
123    }
124
125    if (numRoles > 0) {
126        OMX_U8 **array = new OMX_U8 *[numRoles];
127        for (OMX_U32 i = 0; i < numRoles; ++i) {
128            array[i] = new OMX_U8[OMX_MAX_STRINGNAME_SIZE];
129        }
130
131        err = (*mGetRolesOfComponentHandle)(
132                const_cast<OMX_STRING>(name), &numRoles, array);
133
134        for (OMX_U32 i = 0; i < numRoles; ++i) {
135            if (err == OMX_ErrorNone) {
136                String8 s((const char *)array[i]);
137                roles->push(s);
138            }
139
140            delete[] array[i];
141            array[i] = NULL;
142        }
143
144        delete[] array;
145        array = NULL;
146    }
147
148    return err;
149}
150
151}  // namespace android
152