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 24 namespace android { 25 26 static const char kPrefix[] = "7x30."; 27 28 static void AddPrefix(char *name) { 29 CHECK(!strncmp("OMX.qcom.", name, 9)); 30 String8 tmp(name, 9); 31 tmp.append(kPrefix); 32 tmp.append(&name[9]); 33 strcpy(name, tmp.string()); 34 } 35 36 static void RemovePrefix(const char *name, String8 *out) { 37 out->setTo(name, 9); // "OMX.qcom." 38 out->append(&name[9 + strlen(kPrefix)]); 39 } 40 41 OMXPluginBase *createOMXPlugin() { 42 return new QComOMXPlugin; 43 } 44 45 QComOMXPlugin::QComOMXPlugin() 46 : mLibHandle(dlopen("libOmxCore.so", RTLD_NOW)), 47 mInit(NULL), 48 mDeinit(NULL), 49 mComponentNameEnum(NULL), 50 mGetHandle(NULL), 51 mFreeHandle(NULL), 52 mGetRolesOfComponentHandle(NULL) { 53 if (mLibHandle != NULL) { 54 mInit = (InitFunc)dlsym(mLibHandle, "OMX_Init"); 55 mDeinit = (DeinitFunc)dlsym(mLibHandle, "OMX_DeInit"); 56 57 mComponentNameEnum = 58 (ComponentNameEnumFunc)dlsym(mLibHandle, "OMX_ComponentNameEnum"); 59 60 mGetHandle = (GetHandleFunc)dlsym(mLibHandle, "OMX_GetHandle"); 61 mFreeHandle = (FreeHandleFunc)dlsym(mLibHandle, "OMX_FreeHandle"); 62 63 mGetRolesOfComponentHandle = 64 (GetRolesOfComponentFunc)dlsym( 65 mLibHandle, "OMX_GetRolesOfComponent"); 66 67 (*mInit)(); 68 } 69 } 70 71 QComOMXPlugin::~QComOMXPlugin() { 72 if (mLibHandle != NULL) { 73 (*mDeinit)(); 74 75 dlclose(mLibHandle); 76 mLibHandle = NULL; 77 } 78 } 79 80 OMX_ERRORTYPE QComOMXPlugin::makeComponentInstance( 81 const char *name, 82 const OMX_CALLBACKTYPE *callbacks, 83 OMX_PTR appData, 84 OMX_COMPONENTTYPE **component) { 85 if (mLibHandle == NULL) { 86 return OMX_ErrorUndefined; 87 } 88 89 String8 tmp; 90 RemovePrefix(name, &tmp); 91 name = tmp.string(); 92 93 return (*mGetHandle)( 94 reinterpret_cast<OMX_HANDLETYPE *>(component), 95 const_cast<char *>(name), 96 appData, const_cast<OMX_CALLBACKTYPE *>(callbacks)); 97 } 98 99 OMX_ERRORTYPE QComOMXPlugin::destroyComponentInstance( 100 OMX_COMPONENTTYPE *component) { 101 if (mLibHandle == NULL) { 102 return OMX_ErrorUndefined; 103 } 104 105 return (*mFreeHandle)(reinterpret_cast<OMX_HANDLETYPE *>(component)); 106 } 107 108 OMX_ERRORTYPE QComOMXPlugin::enumerateComponents( 109 OMX_STRING name, 110 size_t size, 111 OMX_U32 index) { 112 if (mLibHandle == NULL) { 113 return OMX_ErrorUndefined; 114 } 115 116 OMX_ERRORTYPE res = (*mComponentNameEnum)(name, size, index); 117 118 if (res != OMX_ErrorNone) { 119 return res; 120 } 121 122 AddPrefix(name); 123 124 return OMX_ErrorNone; 125 } 126 127 OMX_ERRORTYPE QComOMXPlugin::getRolesOfComponent( 128 const char *name, 129 Vector<String8> *roles) { 130 roles->clear(); 131 132 if (mLibHandle == NULL) { 133 return OMX_ErrorUndefined; 134 } 135 136 String8 tmp; 137 RemovePrefix(name, &tmp); 138 name = tmp.string(); 139 140 OMX_U32 numRoles; 141 OMX_ERRORTYPE err = (*mGetRolesOfComponentHandle)( 142 const_cast<OMX_STRING>(name), &numRoles, NULL); 143 144 if (err != OMX_ErrorNone) { 145 return err; 146 } 147 148 if (numRoles > 0) { 149 OMX_U8 **array = new OMX_U8 *[numRoles]; 150 for (OMX_U32 i = 0; i < numRoles; ++i) { 151 array[i] = new OMX_U8[OMX_MAX_STRINGNAME_SIZE]; 152 } 153 154 OMX_U32 numRoles2; 155 err = (*mGetRolesOfComponentHandle)( 156 const_cast<OMX_STRING>(name), &numRoles2, array); 157 158 CHECK_EQ(err, OMX_ErrorNone); 159 CHECK_EQ(numRoles, numRoles2); 160 161 for (OMX_U32 i = 0; i < numRoles; ++i) { 162 String8 s((const char *)array[i]); 163 roles->push(s); 164 165 delete[] array[i]; 166 array[i] = NULL; 167 } 168 169 delete[] array; 170 array = NULL; 171 } 172 173 return OMX_ErrorNone; 174 } 175 176 } // namespace android 177