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