Home | History | Annotate | Download | only in omx
      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/ADebug.h>
     25 #include <media/stagefright/foundation/AString.h>
     26 
     27 #include <dlfcn.h>
     28 
     29 namespace android {
     30 
     31 static const struct {
     32     const char *mName;
     33     const char *mLibNameSuffix;
     34     const char *mRole;
     35 
     36 } kComponents[] = {
     37     { "OMX.google.aac.decoder", "aacdec", "audio_decoder.aac" },
     38     { "OMX.google.aac.encoder", "aacenc", "audio_encoder.aac" },
     39     { "OMX.google.amrnb.decoder", "amrdec", "audio_decoder.amrnb" },
     40     { "OMX.google.amrnb.encoder", "amrnbenc", "audio_encoder.amrnb" },
     41     { "OMX.google.amrwb.decoder", "amrdec", "audio_decoder.amrwb" },
     42     { "OMX.google.amrwb.encoder", "amrwbenc", "audio_encoder.amrwb" },
     43     { "OMX.google.h264.decoder", "avcdec", "video_decoder.avc" },
     44     { "OMX.google.h264.encoder", "avcenc", "video_encoder.avc" },
     45     { "OMX.google.hevc.decoder", "hevcdec", "video_decoder.hevc" },
     46     { "OMX.google.g711.alaw.decoder", "g711dec", "audio_decoder.g711alaw" },
     47     { "OMX.google.g711.mlaw.decoder", "g711dec", "audio_decoder.g711mlaw" },
     48     { "OMX.google.mpeg2.decoder", "mpeg2dec", "video_decoder.mpeg2" },
     49     { "OMX.google.h263.decoder", "mpeg4dec", "video_decoder.h263" },
     50     { "OMX.google.h263.encoder", "mpeg4enc", "video_encoder.h263" },
     51     { "OMX.google.mpeg4.decoder", "mpeg4dec", "video_decoder.mpeg4" },
     52     { "OMX.google.mpeg4.encoder", "mpeg4enc", "video_encoder.mpeg4" },
     53     { "OMX.google.mp3.decoder", "mp3dec", "audio_decoder.mp3" },
     54     { "OMX.google.vorbis.decoder", "vorbisdec", "audio_decoder.vorbis" },
     55     { "OMX.google.opus.decoder", "opusdec", "audio_decoder.opus" },
     56     { "OMX.google.vp8.decoder", "vpxdec", "video_decoder.vp8" },
     57     { "OMX.google.vp9.decoder", "vpxdec", "video_decoder.vp9" },
     58     { "OMX.google.vp8.encoder", "vpxenc", "video_encoder.vp8" },
     59     { "OMX.google.raw.decoder", "rawdec", "audio_decoder.raw" },
     60     { "OMX.google.flac.encoder", "flacenc", "audio_encoder.flac" },
     61     { "OMX.google.gsm.decoder", "gsmdec", "audio_decoder.gsm" },
     62 };
     63 
     64 static const size_t kNumComponents =
     65     sizeof(kComponents) / sizeof(kComponents[0]);
     66 
     67 SoftOMXPlugin::SoftOMXPlugin() {
     68 }
     69 
     70 OMX_ERRORTYPE SoftOMXPlugin::makeComponentInstance(
     71         const char *name,
     72         const OMX_CALLBACKTYPE *callbacks,
     73         OMX_PTR appData,
     74         OMX_COMPONENTTYPE **component) {
     75     ALOGV("makeComponentInstance '%s'", name);
     76 
     77     for (size_t i = 0; i < kNumComponents; ++i) {
     78         if (strcmp(name, kComponents[i].mName)) {
     79             continue;
     80         }
     81 
     82         AString libName = "libstagefright_soft_";
     83         libName.append(kComponents[i].mLibNameSuffix);
     84         libName.append(".so");
     85 
     86         void *libHandle = dlopen(libName.c_str(), RTLD_NOW);
     87 
     88         if (libHandle == NULL) {
     89             ALOGE("unable to dlopen %s: %s", libName.c_str(), dlerror());
     90 
     91             return OMX_ErrorComponentNotFound;
     92         }
     93 
     94         typedef SoftOMXComponent *(*CreateSoftOMXComponentFunc)(
     95                 const char *, const OMX_CALLBACKTYPE *,
     96                 OMX_PTR, OMX_COMPONENTTYPE **);
     97 
     98         CreateSoftOMXComponentFunc createSoftOMXComponent =
     99             (CreateSoftOMXComponentFunc)dlsym(
    100                     libHandle,
    101                     "_Z22createSoftOMXComponentPKcPK16OMX_CALLBACKTYPE"
    102                     "PvPP17OMX_COMPONENTTYPE");
    103 
    104         if (createSoftOMXComponent == NULL) {
    105             dlclose(libHandle);
    106             libHandle = NULL;
    107 
    108             return OMX_ErrorComponentNotFound;
    109         }
    110 
    111         sp<SoftOMXComponent> codec =
    112             (*createSoftOMXComponent)(name, callbacks, appData, component);
    113 
    114         if (codec == NULL) {
    115             dlclose(libHandle);
    116             libHandle = NULL;
    117 
    118             return OMX_ErrorInsufficientResources;
    119         }
    120 
    121         OMX_ERRORTYPE err = codec->initCheck();
    122         if (err != OMX_ErrorNone) {
    123             dlclose(libHandle);
    124             libHandle = NULL;
    125 
    126             return err;
    127         }
    128 
    129         codec->incStrong(this);
    130         codec->setLibHandle(libHandle);
    131 
    132         return OMX_ErrorNone;
    133     }
    134 
    135     return OMX_ErrorInvalidComponentName;
    136 }
    137 
    138 OMX_ERRORTYPE SoftOMXPlugin::destroyComponentInstance(
    139         OMX_COMPONENTTYPE *component) {
    140     SoftOMXComponent *me =
    141         (SoftOMXComponent *)
    142             ((OMX_COMPONENTTYPE *)component)->pComponentPrivate;
    143 
    144     me->prepareForDestruction();
    145 
    146     void *libHandle = me->libHandle();
    147 
    148     CHECK_EQ(me->getStrongCount(), 1);
    149     me->decStrong(this);
    150     me = NULL;
    151 
    152     dlclose(libHandle);
    153     libHandle = NULL;
    154 
    155     return OMX_ErrorNone;
    156 }
    157 
    158 OMX_ERRORTYPE SoftOMXPlugin::enumerateComponents(
    159         OMX_STRING name,
    160         size_t /* size */,
    161         OMX_U32 index) {
    162     if (index >= kNumComponents) {
    163         return OMX_ErrorNoMore;
    164     }
    165 
    166     strcpy(name, kComponents[index].mName);
    167 
    168     return OMX_ErrorNone;
    169 }
    170 
    171 OMX_ERRORTYPE SoftOMXPlugin::getRolesOfComponent(
    172         const char *name,
    173         Vector<String8> *roles) {
    174     for (size_t i = 0; i < kNumComponents; ++i) {
    175         if (strcmp(name, kComponents[i].mName)) {
    176             continue;
    177         }
    178 
    179         roles->clear();
    180         roles->push(String8(kComponents[i].mRole));
    181 
    182         return OMX_ErrorNone;
    183     }
    184 
    185     return OMX_ErrorInvalidComponentName;
    186 }
    187 
    188 }  // namespace android
    189