SoftOMXPlugin.cpp revision 83f70f4472e9b0cacc5d372eddfcef4b02662d10
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
29namespace android {
30
31static 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", "h264dec", "video_decoder.avc" },
44    { "OMX.google.g711.alaw.decoder", "g711dec", "audio_decoder.g711alaw" },
45    { "OMX.google.g711.mlaw.decoder", "g711dec", "audio_decoder.g711mlaw" },
46    { "OMX.google.h263.decoder", "mpeg4dec", "video_decoder.h263" },
47    { "OMX.google.mpeg4.decoder", "mpeg4dec", "video_decoder.mpeg4" },
48    { "OMX.google.mp3.decoder", "mp3dec", "audio_decoder.mp3" },
49    { "OMX.google.vorbis.decoder", "vorbisdec", "audio_decoder.vorbis" },
50    { "OMX.google.vpx.decoder", "vpxdec", "video_decoder.vpx" },
51};
52
53static const size_t kNumComponents =
54    sizeof(kComponents) / sizeof(kComponents[0]);
55
56SoftOMXPlugin::SoftOMXPlugin() {
57}
58
59OMX_ERRORTYPE SoftOMXPlugin::makeComponentInstance(
60        const char *name,
61        const OMX_CALLBACKTYPE *callbacks,
62        OMX_PTR appData,
63        OMX_COMPONENTTYPE **component) {
64    ALOGV("makeComponentInstance '%s'", name);
65
66    for (size_t i = 0; i < kNumComponents; ++i) {
67        if (strcmp(name, kComponents[i].mName)) {
68            continue;
69        }
70
71        AString libName = "libstagefright_soft_";
72        libName.append(kComponents[i].mLibNameSuffix);
73        libName.append(".so");
74
75        void *libHandle = dlopen(libName.c_str(), RTLD_NOW);
76
77        if (libHandle == NULL) {
78            ALOGE("unable to dlopen %s", libName.c_str());
79
80            return OMX_ErrorComponentNotFound;
81        }
82
83        typedef SoftOMXComponent *(*CreateSoftOMXComponentFunc)(
84                const char *, const OMX_CALLBACKTYPE *,
85                OMX_PTR, OMX_COMPONENTTYPE **);
86
87        CreateSoftOMXComponentFunc createSoftOMXComponent =
88            (CreateSoftOMXComponentFunc)dlsym(
89                    libHandle,
90                    "_Z22createSoftOMXComponentPKcPK16OMX_CALLBACKTYPE"
91                    "PvPP17OMX_COMPONENTTYPE");
92
93        if (createSoftOMXComponent == NULL) {
94            dlclose(libHandle);
95            libHandle = NULL;
96
97            return OMX_ErrorComponentNotFound;
98        }
99
100        sp<SoftOMXComponent> codec =
101            (*createSoftOMXComponent)(name, callbacks, appData, component);
102
103        if (codec == NULL) {
104            dlclose(libHandle);
105            libHandle = NULL;
106
107            return OMX_ErrorInsufficientResources;
108        }
109
110        OMX_ERRORTYPE err = codec->initCheck();
111        if (err != OMX_ErrorNone) {
112            dlclose(libHandle);
113            libHandle = NULL;
114
115            return err;
116        }
117
118        codec->incStrong(this);
119        codec->setLibHandle(libHandle);
120
121        return OMX_ErrorNone;
122    }
123
124    return OMX_ErrorInvalidComponentName;
125}
126
127OMX_ERRORTYPE SoftOMXPlugin::destroyComponentInstance(
128        OMX_COMPONENTTYPE *component) {
129    SoftOMXComponent *me =
130        (SoftOMXComponent *)
131            ((OMX_COMPONENTTYPE *)component)->pComponentPrivate;
132
133    me->prepareForDestruction();
134
135    void *libHandle = me->libHandle();
136
137    CHECK_EQ(me->getStrongCount(), 1);
138    me->decStrong(this);
139    me = NULL;
140
141    dlclose(libHandle);
142    libHandle = NULL;
143
144    return OMX_ErrorNone;
145}
146
147OMX_ERRORTYPE SoftOMXPlugin::enumerateComponents(
148        OMX_STRING name,
149        size_t size,
150        OMX_U32 index) {
151    if (index >= kNumComponents) {
152        return OMX_ErrorNoMore;
153    }
154
155    strcpy(name, kComponents[index].mName);
156
157    return OMX_ErrorNone;
158}
159
160OMX_ERRORTYPE SoftOMXPlugin::getRolesOfComponent(
161        const char *name,
162        Vector<String8> *roles) {
163    for (size_t i = 0; i < kNumComponents; ++i) {
164        if (strcmp(name, kComponents[i].mName)) {
165            continue;
166        }
167
168        roles->clear();
169        roles->push(String8(kComponents[i].mRole));
170
171        return OMX_ErrorNone;
172    }
173
174    return OMX_ErrorInvalidComponentName;
175}
176
177}  // namespace android
178