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