1/*
2 * Copyright 2014, 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_INFO_H_
18
19#define MEDIA_CODEC_INFO_H_
20
21#include <binder/Parcel.h>
22#include <media/stagefright/foundation/ABase.h>
23#include <media/stagefright/foundation/AString.h>
24
25#include <sys/types.h>
26#include <utils/Errors.h>
27#include <utils/KeyedVector.h>
28#include <utils/RefBase.h>
29#include <utils/Vector.h>
30#include <utils/StrongPointer.h>
31
32namespace android {
33
34struct AMessage;
35class Parcel;
36
37typedef KeyedVector<AString, AString> CodecSettings;
38
39struct MediaCodecInfo : public RefBase {
40    struct ProfileLevel {
41        uint32_t mProfile;
42        uint32_t mLevel;
43    };
44
45    struct Capabilities : public RefBase {
46        enum {
47            // decoder flags
48            kFlagSupportsAdaptivePlayback = 1 << 0,
49            kFlagSupportsSecurePlayback = 1 << 1,
50            kFlagSupportsTunneledPlayback = 1 << 2,
51
52            // encoder flags
53            kFlagSupportsIntraRefresh = 1 << 0,
54
55        };
56
57        void getSupportedProfileLevels(Vector<ProfileLevel> *profileLevels) const;
58        void getSupportedColorFormats(Vector<uint32_t> *colorFormats) const;
59        uint32_t getFlags() const;
60        const sp<AMessage> getDetails() const;
61
62    protected:
63        Vector<ProfileLevel> mProfileLevels;
64        Vector<uint32_t> mColorFormats;
65        uint32_t mFlags;
66        sp<AMessage> mDetails;
67
68        Capabilities();
69
70    private:
71        // read object from parcel even if object creation fails
72        static sp<Capabilities> FromParcel(const Parcel &parcel);
73        status_t writeToParcel(Parcel *parcel) const;
74
75        DISALLOW_EVIL_CONSTRUCTORS(Capabilities);
76
77        friend class MediaCodecInfo;
78    };
79
80    // Use a subclass to allow setting fields on construction without allowing
81    // to do the same throughout the framework.
82    struct CapabilitiesBuilder : public Capabilities {
83        void addProfileLevel(uint32_t profile, uint32_t level);
84        void addColorFormat(uint32_t format);
85        void addFlags(uint32_t flags);
86    };
87
88    bool isEncoder() const;
89    bool hasQuirk(const char *name) const;
90    void getSupportedMimes(Vector<AString> *mimes) const;
91    const sp<Capabilities> getCapabilitiesFor(const char *mime) const;
92    const char *getCodecName() const;
93
94    /**
95     * Serialization over Binder
96     */
97    static sp<MediaCodecInfo> FromParcel(const Parcel &parcel);
98    status_t writeToParcel(Parcel *parcel) const;
99
100private:
101    // variable set only in constructor - these are accessed by MediaCodecList
102    // to avoid duplication of same variables
103    AString mName;
104    bool mIsEncoder;
105    bool mHasSoleMime; // was initialized with mime
106
107    Vector<AString> mQuirks;
108    KeyedVector<AString, sp<Capabilities> > mCaps;
109
110    sp<Capabilities> mCurrentCaps; // currently initalized capabilities
111
112    ssize_t getCapabilityIndex(const char *mime) const;
113
114    /* Methods used by MediaCodecList to construct the info
115     * object from XML.
116     *
117     * After info object is created:
118     * - additional quirks can be added
119     * - additional mimes can be added
120     *   - OMX codec capabilities can be set for the current mime-type
121     *   - a capability detail can be set for the current mime-type
122     *   - a feature can be set for the current mime-type
123     *   - info object can be completed when parsing of a mime-type is done
124     */
125    MediaCodecInfo(AString name, bool encoder, const char *mime);
126    void addQuirk(const char *name);
127    status_t addMime(const char *mime);
128    status_t updateMime(const char *mime);
129
130    status_t initializeCapabilities(const sp<Capabilities> &caps);
131    void addDetail(const AString &key, const AString &value);
132    void addFeature(const AString &key, int32_t value);
133    void addFeature(const AString &key, const char *value);
134    void removeMime(const char *mime);
135    void complete();
136
137    DISALLOW_EVIL_CONSTRUCTORS(MediaCodecInfo);
138
139    friend class MediaCodecList;
140    friend class MediaCodecListOverridesTest;
141};
142
143}  // namespace android
144
145#endif  // MEDIA_CODEC_INFO_H_
146
147
148