1/*
2 * Copyright 2012, 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#ifndef MEDIA_CODEC_LIST_H_
18
19#define MEDIA_CODEC_LIST_H_
20
21#include <media/stagefright/foundation/ABase.h>
22#include <media/stagefright/foundation/AString.h>
23#include <media/IMediaCodecList.h>
24#include <media/IOMX.h>
25#include <media/MediaCodecInfo.h>
26
27#include <sys/types.h>
28#include <utils/Errors.h>
29#include <utils/KeyedVector.h>
30#include <utils/Vector.h>
31#include <utils/StrongPointer.h>
32
33namespace android {
34
35extern const char *kMaxEncoderInputBuffers;
36
37struct AMessage;
38
39struct MediaCodecList : public BnMediaCodecList {
40    static sp<IMediaCodecList> getInstance();
41
42    virtual ssize_t findCodecByType(
43            const char *type, bool encoder, size_t startIndex = 0) const;
44
45    virtual ssize_t findCodecByName(const char *name) const;
46
47    virtual size_t countCodecs() const;
48
49    virtual sp<MediaCodecInfo> getCodecInfo(size_t index) const {
50        if (index >= mCodecInfos.size()) {
51            ALOGE("b/24445127");
52            return NULL;
53        }
54        return mCodecInfos.itemAt(index);
55    }
56
57    virtual const sp<AMessage> getGlobalSettings() const;
58
59    // to be used by MediaPlayerService alone
60    static sp<IMediaCodecList> getLocalInstance();
61
62    // only to be used by getLocalInstance
63    static void *profilerThreadWrapper(void * /*arg*/);
64
65    // only to be used by MediaPlayerService
66    void parseTopLevelXMLFile(const char *path, bool ignore_errors = false);
67
68    enum Flags {
69        kPreferSoftwareCodecs   = 1,
70        kHardwareCodecsOnly     = 2,
71    };
72
73    static void findMatchingCodecs(
74            const char *mime,
75            bool createEncoder,
76            uint32_t flags,
77            Vector<AString> *matching);
78
79    static uint32_t getQuirksFor(const char *mComponentName);
80
81    static bool isSoftwareCodec(const AString &componentName);
82
83
84private:
85    class BinderDeathObserver : public IBinder::DeathRecipient {
86        void binderDied(const wp<IBinder> &the_late_who __unused);
87    };
88
89    static sp<BinderDeathObserver> sBinderDeathObserver;
90
91    enum Section {
92        SECTION_TOPLEVEL,
93        SECTION_SETTINGS,
94        SECTION_DECODERS,
95        SECTION_DECODER,
96        SECTION_DECODER_TYPE,
97        SECTION_ENCODERS,
98        SECTION_ENCODER,
99        SECTION_ENCODER_TYPE,
100        SECTION_INCLUDE,
101    };
102
103    static sp<IMediaCodecList> sCodecList;
104    static sp<IMediaCodecList> sRemoteList;
105
106    status_t mInitCheck;
107    Section mCurrentSection;
108    bool mUpdate;
109    Vector<Section> mPastSections;
110    int32_t mDepth;
111    AString mHrefBase;
112
113    sp<AMessage> mGlobalSettings;
114    KeyedVector<AString, CodecSettings> mOverrides;
115
116    Vector<sp<MediaCodecInfo> > mCodecInfos;
117    sp<MediaCodecInfo> mCurrentInfo;
118    sp<IOMX> mOMX;
119
120    MediaCodecList();
121    ~MediaCodecList();
122
123    status_t initCheck() const;
124    void parseXMLFile(const char *path);
125
126    static void StartElementHandlerWrapper(
127            void *me, const char *name, const char **attrs);
128
129    static void EndElementHandlerWrapper(void *me, const char *name);
130
131    void startElementHandler(const char *name, const char **attrs);
132    void endElementHandler(const char *name);
133
134    status_t includeXMLFile(const char **attrs);
135    status_t addSettingFromAttributes(const char **attrs);
136    status_t addMediaCodecFromAttributes(bool encoder, const char **attrs);
137    void addMediaCodec(bool encoder, const char *name, const char *type = NULL);
138
139    void setCurrentCodecInfo(bool encoder, const char *name, const char *type);
140
141    status_t addQuirk(const char **attrs);
142    status_t addTypeFromAttributes(const char **attrs);
143    status_t addLimit(const char **attrs);
144    status_t addFeature(const char **attrs);
145    void addType(const char *name);
146
147    status_t initializeCapabilities(const char *type);
148
149    DISALLOW_EVIL_CONSTRUCTORS(MediaCodecList);
150};
151
152}  // namespace android
153
154#endif  // MEDIA_CODEC_LIST_H_
155
156