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 "QComOMXPlugin.h"
18
19#include <dlfcn.h>
20
21#include <media/hardware/HardwareAPI.h>
22
23namespace android {
24
25OMXPluginBase *createOMXPlugin() {
26    return new QComOMXPlugin;
27}
28
29QComOMXPlugin::QComOMXPlugin()
30    : mLibHandle(dlopen("libOmxCore.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, "OMX_Init");
39        mDeinit = (DeinitFunc)dlsym(mLibHandle, "OMX_Deinit");
40
41        mComponentNameEnum =
42            (ComponentNameEnumFunc)dlsym(mLibHandle, "OMX_ComponentNameEnum");
43
44        mGetHandle = (GetHandleFunc)dlsym(mLibHandle, "OMX_GetHandle");
45        mFreeHandle = (FreeHandleFunc)dlsym(mLibHandle, "OMX_FreeHandle");
46
47        mGetRolesOfComponentHandle =
48            (GetRolesOfComponentFunc)dlsym(
49                    mLibHandle, "OMX_GetRolesOfComponent");
50
51        (*mInit)();
52    }
53}
54
55QComOMXPlugin::~QComOMXPlugin() {
56    if (mLibHandle != NULL) {
57        (*mDeinit)();
58
59        dlclose(mLibHandle);
60        mLibHandle = NULL;
61    }
62}
63
64OMX_ERRORTYPE QComOMXPlugin::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 QComOMXPlugin::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 QComOMXPlugin::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 QComOMXPlugin::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) {
127	  return err;
128	}
129
130	if (numRoles2 != numRoles) {
131	  return err;
132	}
133
134        for (OMX_U32 i = 0; i < numRoles; ++i) {
135            String8 s((const char *)array[i]);
136            roles->push(s);
137
138            delete[] array[i];
139            array[i] = NULL;
140        }
141
142        delete[] array;
143        array = NULL;
144    }
145
146    return OMX_ErrorNone;
147}
148
149}  // namespace android
150