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