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