OSDN Git Service

stagefright-plugins: Fix memory leak
[android-x86/external-stagefright-plugins.git] / omx / FFmpegOMXPlugin.cpp
1 /*
2  * Copyright (C) 2015 The CyanogenMod 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 "FFmpegOMXPlugin"
19 #include <utils/Log.h>
20
21 #include "include/SoftOMXComponent.h"
22
23 #include <media/stagefright/foundation/ADebug.h>
24 #include <media/stagefright/foundation/AString.h>
25
26 #include "FFmpegOMXPlugin.h"
27 #include "FFmpegComponents.h"
28 #include "SoftFFmpegAudio.h"
29 #include "SoftFFmpegVideo.h"
30
31 namespace android {
32
33 OMXPluginBase *createOMXPlugin() {
34     return new FFmpegOMXPlugin();
35 }
36
37 FFmpegOMXPlugin::FFmpegOMXPlugin() {
38 }
39
40 OMX_ERRORTYPE FFmpegOMXPlugin::makeComponentInstance(
41         const char *name,
42         const OMX_CALLBACKTYPE *callbacks,
43         OMX_PTR appData,
44         OMX_COMPONENTTYPE **component) {
45
46     ALOGV("makeComponentInstance '%s'", name);
47
48     sp<SoftOMXComponent> codec;
49
50     for (size_t i = 0; i < kNumAudioComponents; ++i) {
51         if (!strcasecmp(name, android::kAudioComponents[i].mName)) {
52             codec = SoftFFmpegAudio::createSoftOMXComponent(name, callbacks, appData, component);
53             break;
54         }
55     }
56
57     if (codec == NULL) {
58         for (size_t i = 0; i < kNumVideoComponents; ++i) {
59             if (!strcasecmp(name, android::kVideoComponents[i].mName)) {
60                 codec = SoftFFmpegVideo::createSoftOMXComponent(name, callbacks, appData, component);
61                 break;
62             }
63         }
64     }
65
66     if (codec != NULL) {
67         OMX_ERRORTYPE err = codec->initCheck();
68         if (err != OMX_ErrorNone) {
69             return err;
70         }
71
72         codec->incStrong(this);
73
74         return OMX_ErrorNone;
75     }
76
77     return OMX_ErrorInvalidComponentName;
78 }
79
80 OMX_ERRORTYPE FFmpegOMXPlugin::destroyComponentInstance(
81         OMX_COMPONENTTYPE *component) {
82     SoftOMXComponent *me =
83         (SoftOMXComponent *)
84             ((OMX_COMPONENTTYPE *)component)->pComponentPrivate;
85
86     me->prepareForDestruction();
87
88     void *libHandle = me->libHandle();
89
90     CHECK_EQ(me->getStrongCount(), 1);
91     me->decStrong(this);
92     me = NULL;
93
94     return OMX_ErrorNone;
95 }
96
97 OMX_ERRORTYPE FFmpegOMXPlugin::enumerateComponents(
98         OMX_STRING name,
99         size_t /* size */,
100         OMX_U32 index) {
101     if (index >= kNumComponents) {
102         return OMX_ErrorNoMore;
103     }
104
105     if (index < kNumAudioComponents) {
106         strcpy(name, kAudioComponents[index].mName);
107     } else {
108         strcpy(name, kVideoComponents[index - kNumAudioComponents].mName);
109     }
110
111     return OMX_ErrorNone;
112 }
113
114 OMX_ERRORTYPE FFmpegOMXPlugin::getRolesOfComponent(
115         const char *name,
116         Vector<String8> *roles) {
117
118     for (size_t i = 0; i < kNumAudioComponents; ++i) {
119         if (strcmp(name, kAudioComponents[i].mName)) {
120             continue;
121         }
122
123         roles->clear();
124         roles->push(String8(kAudioComponents[i].mRole));
125
126         return OMX_ErrorNone;
127     }
128
129     for (size_t i = 0; i < kNumVideoComponents; ++i) {
130         if (strcmp(name, kVideoComponents[i].mName)) {
131             continue;
132         }
133
134         roles->clear();
135         roles->push(String8(kVideoComponents[i].mRole));
136
137         return OMX_ErrorNone;
138     }
139
140     return OMX_ErrorInvalidComponentName;
141 }
142
143 }  // namespace android