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