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