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