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