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