Andreas Huber | 4b3913a | 2011-05-11 14:13:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "SoftOMXPlugin" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include "SoftOMXPlugin.h" |
| 22 | #include "include/SoftOMXComponent.h" |
| 23 | |
| 24 | #include <media/stagefright/foundation/AString.h> |
| 25 | |
| 26 | #include <dlfcn.h> |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | static const struct { |
| 31 | const char *mName; |
| 32 | const char *mLibNameSuffix; |
| 33 | const char *mRole; |
| 34 | |
| 35 | } kComponents[] = { |
| 36 | { "OMX.google.aac.decoder", "aacdec", "audio_decoder.aac" }, |
| 37 | { "OMX.google.amrnb.decoder", "amrdec", "audio_decoder.amrnb" }, |
| 38 | { "OMX.google.amrwb.decoder", "amrdec", "audio_decoder.amrwb" }, |
| 39 | { "OMX.google.avc.decoder", "avcdec", "video_decoder.avc" }, |
| 40 | { "OMX.google.g711.alaw.decoder", "g711dec", "audio_decoder.g711alaw" }, |
| 41 | { "OMX.google.g711.mlaw.decoder", "g711dec", "audio_decoder.g711mlaw" }, |
| 42 | { "OMX.google.h263.decoder", "mpeg4dec", "video_decoder.h263" }, |
| 43 | { "OMX.google.mpeg4.decoder", "mpeg4dec", "video_decoder.mpeg4" }, |
| 44 | { "OMX.google.mp3.decoder", "mp3dec", "audio_decoder.mp3" }, |
| 45 | { "OMX.google.vorbis.decoder", "vorbisdec", "audio_decoder.vorbis" }, |
| 46 | { "OMX.google.vpx.decoder", "vpxdec", "video_decoder.vpx" }, |
| 47 | }; |
| 48 | |
| 49 | static const size_t kNumComponents = |
| 50 | sizeof(kComponents) / sizeof(kComponents[0]); |
| 51 | |
| 52 | SoftOMXPlugin::SoftOMXPlugin() { |
| 53 | } |
| 54 | |
| 55 | OMX_ERRORTYPE SoftOMXPlugin::makeComponentInstance( |
| 56 | const char *name, |
| 57 | const OMX_CALLBACKTYPE *callbacks, |
| 58 | OMX_PTR appData, |
| 59 | OMX_COMPONENTTYPE **component) { |
| 60 | LOGV("makeComponentInstance '%s'", name); |
| 61 | |
| 62 | for (size_t i = 0; i < kNumComponents; ++i) { |
| 63 | if (strcmp(name, kComponents[i].mName)) { |
| 64 | continue; |
| 65 | } |
| 66 | |
| 67 | AString libName = "libstagefright_soft_"; |
| 68 | libName.append(kComponents[i].mLibNameSuffix); |
| 69 | libName.append(".so"); |
| 70 | |
| 71 | void *libHandle = dlopen(libName.c_str(), RTLD_NOW); |
| 72 | |
| 73 | if (libHandle == NULL) { |
| 74 | LOGE("unable to dlopen %s", libName.c_str()); |
| 75 | |
| 76 | return OMX_ErrorComponentNotFound; |
| 77 | } |
| 78 | |
| 79 | typedef SoftOMXComponent *(*CreateSoftOMXComponentFunc)( |
| 80 | const char *, const OMX_CALLBACKTYPE *, |
| 81 | OMX_PTR, OMX_COMPONENTTYPE **); |
| 82 | |
| 83 | CreateSoftOMXComponentFunc createSoftOMXComponent = |
| 84 | (CreateSoftOMXComponentFunc)dlsym( |
| 85 | libHandle, |
| 86 | "_Z22createSoftOMXComponentPKcPK16OMX_CALLBACKTYPE" |
| 87 | "PvPP17OMX_COMPONENTTYPE"); |
| 88 | |
| 89 | if (createSoftOMXComponent == NULL) { |
| 90 | dlclose(libHandle); |
| 91 | libHandle = NULL; |
| 92 | |
| 93 | return OMX_ErrorComponentNotFound; |
| 94 | } |
| 95 | |
| 96 | sp<SoftOMXComponent> codec = |
| 97 | (*createSoftOMXComponent)(name, callbacks, appData, component); |
| 98 | |
| 99 | if (codec == NULL) { |
| 100 | dlclose(libHandle); |
| 101 | libHandle = NULL; |
| 102 | |
| 103 | return OMX_ErrorInsufficientResources; |
| 104 | } |
| 105 | |
| 106 | OMX_ERRORTYPE err = codec->initCheck(); |
| 107 | if (err != OMX_ErrorNone) { |
| 108 | dlclose(libHandle); |
| 109 | libHandle = NULL; |
| 110 | |
| 111 | return err; |
| 112 | } |
| 113 | |
| 114 | codec->incStrong(this); |
| 115 | codec->setLibHandle(libHandle); |
| 116 | |
| 117 | return OMX_ErrorNone; |
| 118 | } |
| 119 | |
| 120 | return OMX_ErrorInvalidComponentName; |
| 121 | } |
| 122 | |
| 123 | OMX_ERRORTYPE SoftOMXPlugin::destroyComponentInstance( |
| 124 | OMX_COMPONENTTYPE *component) { |
| 125 | SoftOMXComponent *me = |
| 126 | (SoftOMXComponent *) |
| 127 | ((OMX_COMPONENTTYPE *)component)->pComponentPrivate; |
| 128 | |
| 129 | void *libHandle = me->libHandle(); |
| 130 | |
| 131 | me->decStrong(this); |
| 132 | me = NULL; |
| 133 | |
| 134 | dlclose(libHandle); |
| 135 | libHandle = NULL; |
| 136 | |
| 137 | return OMX_ErrorNone; |
| 138 | } |
| 139 | |
| 140 | OMX_ERRORTYPE SoftOMXPlugin::enumerateComponents( |
| 141 | OMX_STRING name, |
| 142 | size_t size, |
| 143 | OMX_U32 index) { |
| 144 | if (index >= kNumComponents) { |
| 145 | return OMX_ErrorNoMore; |
| 146 | } |
| 147 | |
| 148 | strcpy(name, kComponents[index].mName); |
| 149 | |
| 150 | return OMX_ErrorNone; |
| 151 | } |
| 152 | |
| 153 | OMX_ERRORTYPE SoftOMXPlugin::getRolesOfComponent( |
| 154 | const char *name, |
| 155 | Vector<String8> *roles) { |
| 156 | for (size_t i = 0; i < kNumComponents; ++i) { |
| 157 | if (strcmp(name, kComponents[i].mName)) { |
| 158 | continue; |
| 159 | } |
| 160 | |
| 161 | roles->clear(); |
| 162 | roles->push(String8(kComponents[i].mRole)); |
| 163 | |
| 164 | return OMX_ErrorNone; |
| 165 | } |
| 166 | |
| 167 | return OMX_ErrorInvalidComponentName; |
| 168 | } |
| 169 | |
| 170 | } // namespace android |