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