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