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