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 <media/stagefright/omx/SoftOMXPlugin.h>
22#include <media/stagefright/omx/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    // two choices for aac decoding.
38    // configurable in media/libstagefright/data/media_codecs_google_audio.xml
39    // default implementation
40    { "OMX.google.aac.decoder", "aacdec", "audio_decoder.aac" },
41    // alternate implementation
42    { "OMX.google.xaac.decoder", "xaacdec", "audio_decoder.aac" },
43    { "OMX.google.aac.encoder", "aacenc", "audio_encoder.aac" },
44    { "OMX.google.amrnb.decoder", "amrdec", "audio_decoder.amrnb" },
45    { "OMX.google.amrnb.encoder", "amrnbenc", "audio_encoder.amrnb" },
46    { "OMX.google.amrwb.decoder", "amrdec", "audio_decoder.amrwb" },
47    { "OMX.google.amrwb.encoder", "amrwbenc", "audio_encoder.amrwb" },
48    { "OMX.google.h264.decoder", "avcdec", "video_decoder.avc" },
49    { "OMX.google.h264.encoder", "avcenc", "video_encoder.avc" },
50    { "OMX.google.hevc.decoder", "hevcdec", "video_decoder.hevc" },
51    { "OMX.google.g711.alaw.decoder", "g711dec", "audio_decoder.g711alaw" },
52    { "OMX.google.g711.mlaw.decoder", "g711dec", "audio_decoder.g711mlaw" },
53    { "OMX.google.mpeg2.decoder", "mpeg2dec", "video_decoder.mpeg2" },
54    { "OMX.google.h263.decoder", "mpeg4dec", "video_decoder.h263" },
55    { "OMX.google.h263.encoder", "mpeg4enc", "video_encoder.h263" },
56    { "OMX.google.mpeg4.decoder", "mpeg4dec", "video_decoder.mpeg4" },
57    { "OMX.google.mpeg4.encoder", "mpeg4enc", "video_encoder.mpeg4" },
58    { "OMX.google.mp3.decoder", "mp3dec", "audio_decoder.mp3" },
59    { "OMX.google.vorbis.decoder", "vorbisdec", "audio_decoder.vorbis" },
60    { "OMX.google.opus.decoder", "opusdec", "audio_decoder.opus" },
61    { "OMX.google.vp8.decoder", "vpxdec", "video_decoder.vp8" },
62    { "OMX.google.vp9.decoder", "vpxdec", "video_decoder.vp9" },
63    { "OMX.google.vp8.encoder", "vpxenc", "video_encoder.vp8" },
64    { "OMX.google.vp9.encoder", "vpxenc", "video_encoder.vp9" },
65    { "OMX.google.raw.decoder", "rawdec", "audio_decoder.raw" },
66    { "OMX.google.flac.decoder", "flacdec", "audio_decoder.flac" },
67    { "OMX.google.flac.encoder", "flacenc", "audio_encoder.flac" },
68    { "OMX.google.gsm.decoder", "gsmdec", "audio_decoder.gsm" },
69};
70
71static const size_t kNumComponents =
72    sizeof(kComponents) / sizeof(kComponents[0]);
73
74SoftOMXPlugin::SoftOMXPlugin() {
75}
76
77OMX_ERRORTYPE SoftOMXPlugin::makeComponentInstance(
78        const char *name,
79        const OMX_CALLBACKTYPE *callbacks,
80        OMX_PTR appData,
81        OMX_COMPONENTTYPE **component) {
82    ALOGV("makeComponentInstance '%s'", name);
83
84    for (size_t i = 0; i < kNumComponents; ++i) {
85        if (strcmp(name, kComponents[i].mName)) {
86            continue;
87        }
88
89        AString libName = "libstagefright_soft_";
90        libName.append(kComponents[i].mLibNameSuffix);
91        libName.append(".so");
92
93        // RTLD_NODELETE means we keep the shared library around forever.
94        // this eliminates thrashing during sequences like loading soundpools.
95        // It also leaves the rest of the logic around the dlopen()/dlclose()
96        // calls in this file unchanged.
97        //
98        // Implications of the change:
99        // -- the codec process (where this happens) will have a slightly larger
100        //    long-term memory footprint as it accumulates the loaded shared libraries.
101        //    This is expected to be a small amount of memory.
102        // -- plugin codecs can no longer (and never should have) depend on a
103        //    free reset of any static data as the library would have crossed
104        //    a dlclose/dlopen cycle.
105        //
106
107        void *libHandle = dlopen(libName.c_str(), RTLD_NOW|RTLD_NODELETE);
108
109        if (libHandle == NULL) {
110            ALOGE("unable to dlopen %s: %s", libName.c_str(), dlerror());
111
112            return OMX_ErrorComponentNotFound;
113        }
114
115        typedef SoftOMXComponent *(*CreateSoftOMXComponentFunc)(
116                const char *, const OMX_CALLBACKTYPE *,
117                OMX_PTR, OMX_COMPONENTTYPE **);
118
119        CreateSoftOMXComponentFunc createSoftOMXComponent =
120            (CreateSoftOMXComponentFunc)dlsym(
121                    libHandle,
122                    "_Z22createSoftOMXComponentPKcPK16OMX_CALLBACKTYPE"
123                    "PvPP17OMX_COMPONENTTYPE");
124
125        if (createSoftOMXComponent == NULL) {
126            dlclose(libHandle);
127            libHandle = NULL;
128
129            return OMX_ErrorComponentNotFound;
130        }
131
132        sp<SoftOMXComponent> codec =
133            (*createSoftOMXComponent)(name, callbacks, appData, component);
134
135        if (codec == NULL) {
136            dlclose(libHandle);
137            libHandle = NULL;
138
139            return OMX_ErrorInsufficientResources;
140        }
141
142        OMX_ERRORTYPE err = codec->initCheck();
143        if (err != OMX_ErrorNone) {
144            dlclose(libHandle);
145            libHandle = NULL;
146
147            return err;
148        }
149
150        codec->incStrong(this);
151        codec->setLibHandle(libHandle);
152
153        return OMX_ErrorNone;
154    }
155
156    return OMX_ErrorInvalidComponentName;
157}
158
159OMX_ERRORTYPE SoftOMXPlugin::destroyComponentInstance(
160        OMX_COMPONENTTYPE *component) {
161    SoftOMXComponent *me =
162        (SoftOMXComponent *)
163            ((OMX_COMPONENTTYPE *)component)->pComponentPrivate;
164
165    me->prepareForDestruction();
166
167    void *libHandle = me->libHandle();
168
169    CHECK_EQ(me->getStrongCount(), 1);
170    me->decStrong(this);
171    me = NULL;
172
173    dlclose(libHandle);
174    libHandle = NULL;
175
176    return OMX_ErrorNone;
177}
178
179OMX_ERRORTYPE SoftOMXPlugin::enumerateComponents(
180        OMX_STRING name,
181        size_t /* size */,
182        OMX_U32 index) {
183    if (index >= kNumComponents) {
184        return OMX_ErrorNoMore;
185    }
186
187    strcpy(name, kComponents[index].mName);
188
189    return OMX_ErrorNone;
190}
191
192OMX_ERRORTYPE SoftOMXPlugin::getRolesOfComponent(
193        const char *name,
194        Vector<String8> *roles) {
195    for (size_t i = 0; i < kNumComponents; ++i) {
196        if (strcmp(name, kComponents[i].mName)) {
197            continue;
198        }
199
200        roles->clear();
201        roles->push(String8(kComponents[i].mRole));
202
203        return OMX_ErrorNone;
204    }
205
206    return OMX_ErrorInvalidComponentName;
207}
208
209}  // namespace android
210