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