Home | History | Annotate | Download | only in stagefright
      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/MediaCodecInfo.h>
     25 
     26 #include <sys/types.h>
     27 #include <utils/Errors.h>
     28 #include <utils/KeyedVector.h>
     29 #include <utils/Vector.h>
     30 #include <utils/StrongPointer.h>
     31 
     32 namespace android {
     33 
     34 extern const char *kMaxEncoderInputBuffers;
     35 
     36 struct AMessage;
     37 
     38 struct MediaCodecListBuilderBase;
     39 
     40 struct MediaCodecList : public BnMediaCodecList {
     41     static sp<IMediaCodecList> getInstance();
     42 
     43     virtual ssize_t findCodecByType(
     44             const char *type, bool encoder, size_t startIndex = 0) const;
     45 
     46     virtual ssize_t findCodecByName(const char *name) const;
     47 
     48     virtual size_t countCodecs() const;
     49 
     50     virtual sp<MediaCodecInfo> getCodecInfo(size_t index) const {
     51         if (index >= mCodecInfos.size()) {
     52             ALOGE("b/24445127");
     53             return NULL;
     54         }
     55         return mCodecInfos[index];
     56     }
     57 
     58     virtual const sp<AMessage> getGlobalSettings() const;
     59 
     60     // to be used by MediaPlayerService alone
     61     static sp<IMediaCodecList> getLocalInstance();
     62 
     63     // only to be used by getLocalInstance
     64     static void *profilerThreadWrapper(void * /*arg*/);
     65 
     66     enum Flags {
     67         kPreferSoftwareCodecs   = 1,
     68         kHardwareCodecsOnly     = 2,
     69     };
     70 
     71     static void findMatchingCodecs(
     72             const char *mime,
     73             bool createEncoder,
     74             uint32_t flags,
     75             Vector<AString> *matchingCodecs,
     76             Vector<AString> *owners = nullptr);
     77 
     78     static bool isSoftwareCodec(const AString &componentName);
     79 
     80 private:
     81     class BinderDeathObserver : public IBinder::DeathRecipient {
     82         void binderDied(const wp<IBinder> &the_late_who __unused);
     83     };
     84 
     85     static sp<BinderDeathObserver> sBinderDeathObserver;
     86 
     87     static sp<IMediaCodecList> sCodecList;
     88     static sp<IMediaCodecList> sRemoteList;
     89 
     90     status_t mInitCheck;
     91 
     92     sp<AMessage> mGlobalSettings;
     93     std::vector<sp<MediaCodecInfo> > mCodecInfos;
     94 
     95     /**
     96      * This constructor will call `buildMediaCodecList()` from the given
     97      * `MediaCodecListBuilderBase` object.
     98      */
     99     MediaCodecList(MediaCodecListBuilderBase* builder);
    100 
    101     ~MediaCodecList();
    102 
    103     status_t initCheck() const;
    104 
    105     MediaCodecList(const MediaCodecList&) = delete;
    106     MediaCodecList& operator=(const MediaCodecList&) = delete;
    107 
    108     friend MediaCodecListWriter;
    109 };
    110 
    111 /**
    112  * This class is to be used by a `MediaCodecListBuilderBase` instance to add
    113  * information to the associated `MediaCodecList` object.
    114  */
    115 struct MediaCodecListWriter {
    116     /**
    117      * Add a key-value pair to a `MediaCodecList`'s global settings.
    118      *
    119      * @param key Key.
    120      * @param value Value.
    121      */
    122     void addGlobalSetting(const char* key, const char* value);
    123     /**
    124      * Create an add a new `MediaCodecInfo` object to a `MediaCodecList`, and
    125      * return a `MediaCodecInfoWriter` object associated with the newly added
    126      * `MediaCodecInfo`.
    127      *
    128      * @return The `MediaCodecInfoWriter` object associated with the newly
    129      * added `MediaCodecInfo` object.
    130      */
    131     std::unique_ptr<MediaCodecInfoWriter> addMediaCodecInfo();
    132 private:
    133     /**
    134      * The associated `MediaCodecList` object.
    135      */
    136     MediaCodecList* mList;
    137 
    138     /**
    139      * Construct this writer object associated with the given `MediaCodecList`
    140      * object.
    141      *
    142      * @param list The "base" `MediaCodecList` object.
    143      */
    144     MediaCodecListWriter(MediaCodecList* list);
    145 
    146     friend MediaCodecList;
    147 };
    148 
    149 /**
    150  * This interface is to be used by `MediaCodecList` to fill its members with
    151  * appropriate information. `buildMediaCodecList()` will be called from a
    152  * `MediaCodecList` object during its construction.
    153  */
    154 struct MediaCodecListBuilderBase {
    155     /**
    156      * Build the `MediaCodecList` via the given `MediaCodecListWriter` interface.
    157      *
    158      * @param writer The writer interface.
    159      * @return The status of the construction. `NO_ERROR` means success.
    160      */
    161     virtual status_t buildMediaCodecList(MediaCodecListWriter* writer) = 0;
    162 
    163     /**
    164      * The default destructor does nothing.
    165      */
    166     virtual ~MediaCodecListBuilderBase();
    167 };
    168 
    169 }  // namespace android
    170 
    171 #endif  // MEDIA_CODEC_LIST_H_
    172 
    173