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