• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <android-base/macros.h>
22 #include <binder/Parcel.h>
23 #include <media/stagefright/foundation/ABase.h>
24 #include <media/stagefright/foundation/AString.h>
25 
26 #include <sys/types.h>
27 #include <utils/Errors.h>
28 #include <utils/KeyedVector.h>
29 #include <utils/RefBase.h>
30 #include <utils/Vector.h>
31 #include <utils/StrongPointer.h>
32 
33 #include <type_traits>
34 
35 namespace android {
36 
37 struct AMessage;
38 class Parcel;
39 
40 typedef KeyedVector<AString, AString> CodecSettings;
41 
42 struct MediaCodecInfoWriter;
43 struct MediaCodecListWriter;
44 
45 struct MediaCodecInfo : public RefBase {
46     struct ProfileLevel {
47         uint32_t mProfile;
48         uint32_t mLevel;
49         bool operator <(const ProfileLevel &o) const {
50             return mProfile < o.mProfile || (mProfile == o.mProfile && mLevel < o.mLevel);
51         }
52     };
53 
54     struct CapabilitiesWriter;
55 
56     enum Attributes : int32_t {
57         // attribute flags
58         kFlagIsEncoder = 1 << 0,
59         kFlagIsVendor = 1 << 1,
60         kFlagIsSoftwareOnly = 1 << 2,
61         kFlagIsHardwareAccelerated = 1 << 3,
62     };
63 
64     struct Capabilities : public RefBase {
65         constexpr static char FEATURE_ADAPTIVE_PLAYBACK[] = "feature-adaptive-playback";
66         constexpr static char FEATURE_DYNAMIC_TIMESTAMP[] = "feature-dynamic-timestamp";
67         constexpr static char FEATURE_FRAME_PARSING[] = "feature-frame-parsing";
68         constexpr static char FEATURE_INTRA_REFRESH[] = "feature-frame-parsing";
69         constexpr static char FEATURE_MULTIPLE_FRAMES[] = "feature-multiple-frames";
70         constexpr static char FEATURE_SECURE_PLAYBACK[] = "feature-secure-playback";
71         constexpr static char FEATURE_TUNNELED_PLAYBACK[] = "feature-tunneled-playback";
72         constexpr static char FEATURE_DETACHED_SURFACE[] = "feature-detached-surface";
73 
74         /**
75          * Returns the supported levels for each supported profile in a target array.
76          *
77          * @param profileLevels target array for the profile levels.
78          */
79         void getSupportedProfileLevels(Vector<ProfileLevel> *profileLevels) const;
80 
81         /**
82          * Returns the supported color formats in a target array. Only used for video/image
83          * components.
84          *
85          * @param colorFormats target array for the color formats.
86          */
87         void getSupportedColorFormats(Vector<uint32_t> *colorFormats) const;
88 
89         /**
90          * Returns metadata associated with this codec capability.
91          *
92          * This contains:
93          * - features,
94          * - performance data.
95          *
96          * TODO: expose this as separate API-s and wrap here.
97          */
98         const sp<AMessage> getDetails() const;
99 
100     protected:
101         Vector<ProfileLevel> mProfileLevels;
102         SortedVector<ProfileLevel> mProfileLevelsSorted;
103         Vector<uint32_t> mColorFormats;
104         SortedVector<uint32_t> mColorFormatsSorted;
105         sp<AMessage> mDetails;
106 
107         Capabilities();
108 
109     private:
110         // read object from parcel even if object creation fails
111         static sp<Capabilities> FromParcel(const Parcel &parcel);
112         status_t writeToParcel(Parcel *parcel) const;
113 
114         DISALLOW_COPY_AND_ASSIGN(Capabilities);
115 
116         friend struct MediaCodecInfo;
117         friend struct MediaCodecInfoWriter;
118         friend struct CapabilitiesWriter;
119     };
120 
121     /**
122      * This class is used for modifying information inside a `Capabilities`
123      * object. An object of type `CapabilitiesWriter` can be obtained by calling
124      * `MediaCodecInfoWriter::addMediaType()`.
125      */
126     struct CapabilitiesWriter {
127         /**
128          * Add a key-value pair to the list of details. If the key already
129          * exists, the old value will be replaced.
130          *
131          * A pair added by this function will be accessible by
132          * `Capabilities::getDetails()`. Call `AMessage::getString()` with the
133          * same key to retrieve the value.
134          *
135          * @param key The key.
136          * @param value The string value.
137          */
138         void addDetail(const char* key, const char* value);
139         /**
140          * Add a key-value pair to the list of details. If the key already
141          * exists, the old value will be replaced.
142          *
143          * A pair added by this function will be accessible by
144          * `Capabilities::getDetails()`. Call `AMessage::getInt32()` with the
145          * same key to retrieve the value.
146          *
147          * @param key The key.
148          * @param value The `int32_t` value.
149          */
150         void addDetail(const char* key, int32_t value);
151         /**
152          * Removes a key-value pair from the list of details. If the key is not
153          * present, this call does nothing.
154          *
155          * @param key The key.
156          */
157         void removeDetail(const char* key);
158         /**
159          * Add a profile-level pair. If this profile-level pair already exists,
160          * it will be ignored.
161          *
162          * @param profile The "profile" component.
163          * @param level The "level" component.
164          */
165         void addProfileLevel(uint32_t profile, uint32_t level);
166         /**
167          * Add a color format. If this color format already exists, it will be
168          * ignored.
169          *
170          * @param format The color format.
171          */
172         void addColorFormat(uint32_t format);
173 
174     private:
175         /**
176          * The associated `Capabilities` object.
177          */
178         Capabilities* mCap;
179         /**
180          * Construct a writer for the given `Capabilities` object.
181          *
182          * @param cap The `Capabilities` object to be written to.
183          */
184         CapabilitiesWriter(Capabilities* cap);
185 
186         friend MediaCodecInfoWriter;
187     };
188 
isEncoderMediaCodecInfo189     inline bool isEncoder() const {
190         return getAttributes() & kFlagIsEncoder;
191     }
192 
193     Attributes getAttributes() const;
194     void getSupportedMediaTypes(Vector<AString> *mediaTypes) const;
195     const sp<Capabilities> getCapabilitiesFor(const char *mediaType) const;
196     const char *getCodecName() const;
197 
198     /**
199      * Returns a vector containing alternate names for the codec.
200      *
201      * \param aliases the destination array for the aliases. This is cleared.
202      *
203      * Multiple codecs may share alternate names as long as their supported media types are
204      * distinct; however, these will result in different aliases for the MediaCodec user as
205      * the canonical codec has to be resolved without knowing the media type in
206      * MediaCodec::CreateByComponentName.
207      */
208     void getAliases(Vector<AString> *aliases) const;
209 
210     /**
211      * Return the name of the service that hosts the codec. This value is not
212      * visible at the Java level.
213      *
214      * Currently, this is the "instance name" of the IOmx service.
215      */
216     const char *getOwnerName() const;
217 
218     /**
219      * Returns the rank of the component.
220      *
221      * Technically this is defined to be per media type, but that makes ordering the MediaCodecList
222      * impossible as MediaCodecList is ordered by codec name.
223      */
224     uint32_t getRank() const;
225 
226     /**
227      * Serialization over Binder
228      */
229     static sp<MediaCodecInfo> FromParcel(const Parcel &parcel);
230     status_t writeToParcel(Parcel *parcel) const;
231 
232 private:
233     AString mName;
234     AString mOwner;
235     Attributes mAttributes;
236     KeyedVector<AString, sp<Capabilities> > mCaps;
237     Vector<AString> mAliases;
238     uint32_t mRank;
239 
240     ssize_t getCapabilityIndex(const char *mediaType) const;
241 
242     /**
243      * Construct an `MediaCodecInfo` object. After the construction, its
244      * information can be set via an `MediaCodecInfoWriter` object obtained from
245      * `MediaCodecListWriter::addMediaCodecInfo()`.
246      */
247     MediaCodecInfo();
248 
249     DISALLOW_COPY_AND_ASSIGN(MediaCodecInfo);
250 
251     friend class MediaCodecListOverridesTest;
252     friend struct MediaCodecInfoWriter;
253     friend struct MediaCodecListWriter;
254 };
255 
256 /**
257  * This class is to be used by a `MediaCodecListBuilderBase` instance to
258  * populate information inside the associated `MediaCodecInfo` object.
259  *
260  * The only place where an instance of `MediaCodecInfoWriter` can be constructed
261  * is `MediaCodecListWriter::addMediaCodecInfo()`. A `MediaCodecListBuilderBase`
262  * instance should call `MediaCodecListWriter::addMediaCodecInfo()` on the given
263  * `MediaCodecListWriter` object given as an input to
264  * `MediaCodecListBuilderBase::buildMediaCodecList()`.
265  */
266 struct MediaCodecInfoWriter {
267     /**
268      * Set the name of the codec.
269      *
270      * @param name The new name.
271      */
272     void setName(const char* name);
273     /**
274      * Adds an alias (alternate name) for the codec. Multiple codecs can share an alternate name
275      * as long as their supported media types are distinct.
276      *
277      * @param name an alternate name.
278      */
279     void addAlias(const char* name);
280     /**
281      * Set the owner name of the codec.
282      *
283      * This "owner name" is the name of the `IOmx` instance that supports this
284      * codec.
285      *
286      * @param owner The new owner name.
287      */
288     void setOwner(const char* owner);
289     /**
290      * Sets codec attributes.
291      *
292      * @param attributes Codec attributes.
293      */
294     void setAttributes(typename std::underlying_type<MediaCodecInfo::Attributes>::type attributes);
295     /**
296      * Add a media type to an indexed list and return a `CapabilitiesWriter` object
297      * that can be used for modifying the associated `Capabilities`.
298      *
299      * If the media type already exists, this function will return the
300      * `CapabilitiesWriter` associated with the media type.
301      *
302      * @param[in] mediaType The name of a new media type to add.
303      * @return writer The `CapabilitiesWriter` object for modifying the
304      * `Capabilities` associated with the media type. `writer` will be valid
305      * regardless of whether `mediaType` already exists or not.
306      */
307     std::unique_ptr<MediaCodecInfo::CapabilitiesWriter> addMediaType(
308             const char* mediaType);
309     /**
310      * Remove a media type.
311      *
312      * @param mediaType The name of the media type to remove.
313      * @return `true` if `mediaType` is removed; `false` if `mediaType` is not found.
314      */
315     bool removeMediaType(const char* mediaType);
316     /**
317      * Set rank of the codec. MediaCodecList will stable-sort the list according
318      * to rank in non-descending order.
319      *
320      * @param rank The rank of the component.
321      */
322     void setRank(uint32_t rank);
323 private:
324     /**
325      * The associated `MediaCodecInfo`.
326      */
327     MediaCodecInfo* mInfo;
328     /**
329      * Construct the `MediaCodecInfoWriter` object associated with the given
330      * `MediaCodecInfo` object.
331      *
332      * @param info The underlying `MediaCodecInfo` object.
333      */
334     MediaCodecInfoWriter(MediaCodecInfo* info);
335 
336     DISALLOW_COPY_AND_ASSIGN(MediaCodecInfoWriter);
337 
338     friend struct MediaCodecListWriter;
339 };
340 
341 }  // namespace android
342 
343 #endif  // MEDIA_CODEC_INFO_H_
344 
345 
346