Home | History | Annotate | Download | only in media
      1 /*
      2  **
      3  ** Copyright 2010, The Android Open Source Project.
      4  **
      5  ** Licensed under the Apache License, Version 2.0 (the "License");
      6  ** you may not use this file except in compliance with the License.
      7  ** You may obtain a copy of the License at
      8  **
      9  **     http://www.apache.org/licenses/LICENSE-2.0
     10  **
     11  ** Unless required by applicable law or agreed to in writing, software
     12  ** distributed under the License is distributed on an "AS IS" BASIS,
     13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  ** See the License for the specific language governing permissions and
     15  ** limitations under the License.
     16  */
     17 
     18 #ifndef ANDROID_MEDIAPROFILES_H
     19 #define ANDROID_MEDIAPROFILES_H
     20 
     21 #include <utils/threads.h>
     22 #include <media/mediarecorder.h>
     23 
     24 namespace android {
     25 
     26 enum camcorder_quality {
     27     CAMCORDER_QUALITY_LIST_START = 0,
     28     CAMCORDER_QUALITY_LOW  = 0,
     29     CAMCORDER_QUALITY_HIGH = 1,
     30     CAMCORDER_QUALITY_QCIF = 2,
     31     CAMCORDER_QUALITY_CIF = 3,
     32     CAMCORDER_QUALITY_480P = 4,
     33     CAMCORDER_QUALITY_720P = 5,
     34     CAMCORDER_QUALITY_1080P = 6,
     35     CAMCORDER_QUALITY_QVGA = 7,
     36     CAMCORDER_QUALITY_LIST_END = 7,
     37 
     38     CAMCORDER_QUALITY_TIME_LAPSE_LIST_START = 1000,
     39     CAMCORDER_QUALITY_TIME_LAPSE_LOW  = 1000,
     40     CAMCORDER_QUALITY_TIME_LAPSE_HIGH = 1001,
     41     CAMCORDER_QUALITY_TIME_LAPSE_QCIF = 1002,
     42     CAMCORDER_QUALITY_TIME_LAPSE_CIF = 1003,
     43     CAMCORDER_QUALITY_TIME_LAPSE_480P = 1004,
     44     CAMCORDER_QUALITY_TIME_LAPSE_720P = 1005,
     45     CAMCORDER_QUALITY_TIME_LAPSE_1080P = 1006,
     46     CAMCORDER_QUALITY_TIME_LAPSE_QVGA = 1007,
     47     CAMCORDER_QUALITY_TIME_LAPSE_LIST_END = 1007,
     48 };
     49 
     50 /**
     51  *Set CIF as default maximum import and export resolution of video editor.
     52  *The maximum import and export resolutions are platform specific,
     53  *which should be defined in media_profiles.xml.
     54  */
     55 enum videoeditor_capability {
     56     VIDEOEDITOR_DEFAULT_MAX_INPUT_FRAME_WIDTH = 352,
     57     VIDEOEDITOR_DEFUALT_MAX_INPUT_FRAME_HEIGHT = 288,
     58     VIDEOEDITOR_DEFAULT_MAX_OUTPUT_FRAME_WIDTH = 352,
     59     VIDEOEDITOR_DEFUALT_MAX_OUTPUT_FRAME_HEIGHT = 288,
     60 };
     61 
     62 enum video_decoder {
     63     VIDEO_DECODER_WMV,
     64 };
     65 
     66 enum audio_decoder {
     67     AUDIO_DECODER_WMA,
     68 };
     69 
     70 
     71 class MediaProfiles
     72 {
     73 public:
     74 
     75     /**
     76      * Returns the singleton instance for subsequence queries.
     77      * or NULL if error.
     78      */
     79     static MediaProfiles* getInstance();
     80 
     81     /**
     82      * Returns the value for the given param name for the given camera at
     83      * the given quality level, or -1 if error.
     84      *
     85      * Supported param name are:
     86      * duration - the recording duration.
     87      * file.format - output file format. see mediarecorder.h for details
     88      * vid.codec - video encoder. see mediarecorder.h for details.
     89      * aud.codec - audio encoder. see mediarecorder.h for details.
     90      * vid.width - video frame width
     91      * vid.height - video frame height
     92      * vid.fps - video frame rate
     93      * vid.bps - video bit rate
     94      * aud.bps - audio bit rate
     95      * aud.hz - audio sample rate
     96      * aud.ch - number of audio channels
     97      */
     98     int getCamcorderProfileParamByName(const char *name, int cameraId,
     99                                        camcorder_quality quality) const;
    100 
    101     /**
    102      * Returns true if a profile for the given camera at the given quality exists,
    103      * or false if not.
    104      */
    105     bool hasCamcorderProfile(int cameraId, camcorder_quality quality) const;
    106 
    107     /**
    108      * Returns the output file formats supported.
    109      */
    110     Vector<output_format> getOutputFileFormats() const;
    111 
    112     /**
    113      * Returns the video encoders supported.
    114      */
    115     Vector<video_encoder> getVideoEncoders() const;
    116 
    117     /**
    118      * Returns the value for the given param name for the given video encoder
    119      * returned from getVideoEncoderByIndex or -1 if error.
    120      *
    121      * Supported param name are:
    122      * enc.vid.width.min - min video frame width
    123      * enc.vid.width.max - max video frame width
    124      * enc.vid.height.min - min video frame height
    125      * enc.vid.height.max - max video frame height
    126      * enc.vid.bps.min - min bit rate in bits per second
    127      * enc.vid.bps.max - max bit rate in bits per second
    128      * enc.vid.fps.min - min frame rate in frames per second
    129      * enc.vid.fps.max - max frame rate in frames per second
    130      */
    131     int getVideoEncoderParamByName(const char *name, video_encoder codec) const;
    132 
    133     /**
    134      * Returns the value for the given param name for the video editor cap
    135      * param or -1 if error.
    136      * Supported param name are:
    137      * videoeditor.input.width.max - max input video frame width
    138      * videoeditor.input.height.max - max input video frame height
    139      * videoeditor.output.width.max - max output video frame width
    140      * videoeditor.output.height.max - max output video frame height
    141      */
    142     int getVideoEditorCapParamByName(const char *name) const;
    143 
    144     /**
    145      * Returns the value for the given param name for the video editor export codec format
    146      * param or -1 if error.
    147      * Supported param name are:
    148      * videoeditor.export.profile - export video profile
    149      * videoeditor.export.level - export video level
    150      * Supported param codec are:
    151      * 1 for h263
    152      * 2 for h264
    153      * 3 for mpeg4
    154      */
    155     int getVideoEditorExportParamByName(const char *name, int codec) const;
    156 
    157     /**
    158      * Returns the audio encoders supported.
    159      */
    160     Vector<audio_encoder> getAudioEncoders() const;
    161 
    162     /**
    163      * Returns the value for the given param name for the given audio encoder
    164      * returned from getAudioEncoderByIndex or -1 if error.
    165      *
    166      * Supported param name are:
    167      * enc.aud.ch.min - min number of channels
    168      * enc.aud.ch.max - max number of channels
    169      * enc.aud.bps.min - min bit rate in bits per second
    170      * enc.aud.bps.max - max bit rate in bits per second
    171      * enc.aud.hz.min - min sample rate in samples per second
    172      * enc.aud.hz.max - max sample rate in samples per second
    173      */
    174     int getAudioEncoderParamByName(const char *name, audio_encoder codec) const;
    175 
    176     /**
    177       * Returns the video decoders supported.
    178       */
    179     Vector<video_decoder> getVideoDecoders() const;
    180 
    181      /**
    182       * Returns the audio decoders supported.
    183       */
    184     Vector<audio_decoder> getAudioDecoders() const;
    185 
    186     /**
    187      * Returns the number of image encoding quality levels supported.
    188      */
    189     Vector<int> getImageEncodingQualityLevels(int cameraId) const;
    190 
    191     /**
    192      * Returns the start time offset (in ms) for the given camera Id.
    193      * If the given camera Id does not exist, -1 will be returned.
    194      */
    195     int getStartTimeOffsetMs(int cameraId) const;
    196 
    197 private:
    198     enum {
    199         // Camcorder profiles (high/low) and timelapse profiles (high/low)
    200         kNumRequiredProfiles = 4,
    201     };
    202 
    203     MediaProfiles& operator=(const MediaProfiles&);  // Don't call me
    204     MediaProfiles(const MediaProfiles&);             // Don't call me
    205     MediaProfiles() { mVideoEditorCap = NULL; }        // Dummy default constructor
    206     ~MediaProfiles();                                // Don't delete me
    207 
    208     struct VideoCodec {
    209         VideoCodec(video_encoder codec, int bitRate, int frameWidth, int frameHeight, int frameRate)
    210             : mCodec(codec),
    211               mBitRate(bitRate),
    212               mFrameWidth(frameWidth),
    213               mFrameHeight(frameHeight),
    214               mFrameRate(frameRate) {}
    215 
    216         VideoCodec(const VideoCodec& copy) {
    217             mCodec = copy.mCodec;
    218             mBitRate = copy.mBitRate;
    219             mFrameWidth = copy.mFrameWidth;
    220             mFrameHeight = copy.mFrameHeight;
    221             mFrameRate = copy.mFrameRate;
    222         }
    223 
    224         ~VideoCodec() {}
    225 
    226         video_encoder mCodec;
    227         int mBitRate;
    228         int mFrameWidth;
    229         int mFrameHeight;
    230         int mFrameRate;
    231     };
    232 
    233     struct AudioCodec {
    234         AudioCodec(audio_encoder codec, int bitRate, int sampleRate, int channels)
    235             : mCodec(codec),
    236               mBitRate(bitRate),
    237               mSampleRate(sampleRate),
    238               mChannels(channels) {}
    239 
    240         AudioCodec(const AudioCodec& copy) {
    241             mCodec = copy.mCodec;
    242             mBitRate = copy.mBitRate;
    243             mSampleRate = copy.mSampleRate;
    244             mChannels = copy.mChannels;
    245         }
    246 
    247         ~AudioCodec() {}
    248 
    249         audio_encoder mCodec;
    250         int mBitRate;
    251         int mSampleRate;
    252         int mChannels;
    253     };
    254 
    255     struct CamcorderProfile {
    256         CamcorderProfile()
    257             : mCameraId(0),
    258               mFileFormat(OUTPUT_FORMAT_THREE_GPP),
    259               mQuality(CAMCORDER_QUALITY_HIGH),
    260               mDuration(0),
    261               mVideoCodec(0),
    262               mAudioCodec(0) {}
    263 
    264         CamcorderProfile(const CamcorderProfile& copy) {
    265             mCameraId = copy.mCameraId;
    266             mFileFormat = copy.mFileFormat;
    267             mQuality = copy.mQuality;
    268             mDuration = copy.mDuration;
    269             mVideoCodec = new VideoCodec(*copy.mVideoCodec);
    270             mAudioCodec = new AudioCodec(*copy.mAudioCodec);
    271         }
    272 
    273         ~CamcorderProfile() {
    274             delete mVideoCodec;
    275             delete mAudioCodec;
    276         }
    277 
    278         int mCameraId;
    279         output_format mFileFormat;
    280         camcorder_quality mQuality;
    281         int mDuration;
    282         VideoCodec *mVideoCodec;
    283         AudioCodec *mAudioCodec;
    284     };
    285 
    286     struct VideoEncoderCap {
    287         // Ugly constructor
    288         VideoEncoderCap(video_encoder codec,
    289                         int minBitRate, int maxBitRate,
    290                         int minFrameWidth, int maxFrameWidth,
    291                         int minFrameHeight, int maxFrameHeight,
    292                         int minFrameRate, int maxFrameRate)
    293             : mCodec(codec),
    294               mMinBitRate(minBitRate), mMaxBitRate(maxBitRate),
    295               mMinFrameWidth(minFrameWidth), mMaxFrameWidth(maxFrameWidth),
    296               mMinFrameHeight(minFrameHeight), mMaxFrameHeight(maxFrameHeight),
    297               mMinFrameRate(minFrameRate), mMaxFrameRate(maxFrameRate) {}
    298 
    299          ~VideoEncoderCap() {}
    300 
    301         video_encoder mCodec;
    302         int mMinBitRate, mMaxBitRate;
    303         int mMinFrameWidth, mMaxFrameWidth;
    304         int mMinFrameHeight, mMaxFrameHeight;
    305         int mMinFrameRate, mMaxFrameRate;
    306     };
    307 
    308     struct AudioEncoderCap {
    309         // Ugly constructor
    310         AudioEncoderCap(audio_encoder codec,
    311                         int minBitRate, int maxBitRate,
    312                         int minSampleRate, int maxSampleRate,
    313                         int minChannels, int maxChannels)
    314             : mCodec(codec),
    315               mMinBitRate(minBitRate), mMaxBitRate(maxBitRate),
    316               mMinSampleRate(minSampleRate), mMaxSampleRate(maxSampleRate),
    317               mMinChannels(minChannels), mMaxChannels(maxChannels) {}
    318 
    319         ~AudioEncoderCap() {}
    320 
    321         audio_encoder mCodec;
    322         int mMinBitRate, mMaxBitRate;
    323         int mMinSampleRate, mMaxSampleRate;
    324         int mMinChannels, mMaxChannels;
    325     };
    326 
    327     struct VideoDecoderCap {
    328         VideoDecoderCap(video_decoder codec): mCodec(codec) {}
    329         ~VideoDecoderCap() {}
    330 
    331         video_decoder mCodec;
    332     };
    333 
    334     struct AudioDecoderCap {
    335         AudioDecoderCap(audio_decoder codec): mCodec(codec) {}
    336         ~AudioDecoderCap() {}
    337 
    338         audio_decoder mCodec;
    339     };
    340 
    341     struct NameToTagMap {
    342         const char* name;
    343         int tag;
    344     };
    345 
    346     struct ImageEncodingQualityLevels {
    347         int mCameraId;
    348         Vector<int> mLevels;
    349     };
    350     struct ExportVideoProfile {
    351         ExportVideoProfile(int codec, int profile, int level)
    352             :mCodec(codec),mProfile(profile),mLevel(level) {}
    353         ~ExportVideoProfile() {}
    354         int mCodec;
    355         int mProfile;
    356         int mLevel;
    357     };
    358     struct VideoEditorCap {
    359         VideoEditorCap(int inFrameWidth, int inFrameHeight,
    360             int outFrameWidth, int outFrameHeight)
    361             : mMaxInputFrameWidth(inFrameWidth),
    362               mMaxInputFrameHeight(inFrameHeight),
    363               mMaxOutputFrameWidth(outFrameWidth),
    364               mMaxOutputFrameHeight(outFrameHeight) {}
    365 
    366         ~VideoEditorCap() {}
    367 
    368         int mMaxInputFrameWidth;
    369         int mMaxInputFrameHeight;
    370         int mMaxOutputFrameWidth;
    371         int mMaxOutputFrameHeight;
    372     };
    373 
    374     int getCamcorderProfileIndex(int cameraId, camcorder_quality quality) const;
    375     void initRequiredProfileRefs(const Vector<int>& cameraIds);
    376     int getRequiredProfileRefIndex(int cameraId);
    377 
    378     // Debug
    379     static void logVideoCodec(const VideoCodec& codec);
    380     static void logAudioCodec(const AudioCodec& codec);
    381     static void logVideoEncoderCap(const VideoEncoderCap& cap);
    382     static void logAudioEncoderCap(const AudioEncoderCap& cap);
    383     static void logVideoDecoderCap(const VideoDecoderCap& cap);
    384     static void logAudioDecoderCap(const AudioDecoderCap& cap);
    385     static void logVideoEditorCap(const VideoEditorCap& cap);
    386 
    387     // If the xml configuration file does exist, use the settings
    388     // from the xml
    389     static MediaProfiles* createInstanceFromXmlFile(const char *xml);
    390     static output_format createEncoderOutputFileFormat(const char **atts);
    391     static VideoCodec* createVideoCodec(const char **atts, MediaProfiles *profiles);
    392     static AudioCodec* createAudioCodec(const char **atts, MediaProfiles *profiles);
    393     static AudioDecoderCap* createAudioDecoderCap(const char **atts);
    394     static VideoDecoderCap* createVideoDecoderCap(const char **atts);
    395     static VideoEncoderCap* createVideoEncoderCap(const char **atts);
    396     static AudioEncoderCap* createAudioEncoderCap(const char **atts);
    397     static VideoEditorCap* createVideoEditorCap(
    398                 const char **atts, MediaProfiles *profiles);
    399     static ExportVideoProfile* createExportVideoProfile(const char **atts);
    400 
    401     static CamcorderProfile* createCamcorderProfile(
    402                 int cameraId, const char **atts, Vector<int>& cameraIds);
    403 
    404     static int getCameraId(const char **atts);
    405 
    406     void addStartTimeOffset(int cameraId, const char **atts);
    407 
    408     ImageEncodingQualityLevels* findImageEncodingQualityLevels(int cameraId) const;
    409     void addImageEncodingQualityLevel(int cameraId, const char** atts);
    410 
    411     // Customized element tag handler for parsing the xml configuration file.
    412     static void startElementHandler(void *userData, const char *name, const char **atts);
    413 
    414     // If the xml configuration file does not exist, use hard-coded values
    415     static MediaProfiles* createDefaultInstance();
    416 
    417     static CamcorderProfile *createDefaultCamcorderQcifProfile(camcorder_quality quality);
    418     static CamcorderProfile *createDefaultCamcorderCifProfile(camcorder_quality quality);
    419     static void createDefaultCamcorderLowProfiles(
    420             MediaProfiles::CamcorderProfile **lowProfile,
    421             MediaProfiles::CamcorderProfile **lowSpecificProfile);
    422     static void createDefaultCamcorderHighProfiles(
    423             MediaProfiles::CamcorderProfile **highProfile,
    424             MediaProfiles::CamcorderProfile **highSpecificProfile);
    425 
    426     static CamcorderProfile *createDefaultCamcorderTimeLapseQcifProfile(camcorder_quality quality);
    427     static CamcorderProfile *createDefaultCamcorderTimeLapse480pProfile(camcorder_quality quality);
    428     static void createDefaultCamcorderTimeLapseLowProfiles(
    429             MediaProfiles::CamcorderProfile **lowTimeLapseProfile,
    430             MediaProfiles::CamcorderProfile **lowSpecificTimeLapseProfile);
    431     static void createDefaultCamcorderTimeLapseHighProfiles(
    432             MediaProfiles::CamcorderProfile **highTimeLapseProfile,
    433             MediaProfiles::CamcorderProfile **highSpecificTimeLapseProfile);
    434 
    435     static void createDefaultCamcorderProfiles(MediaProfiles *profiles);
    436     static void createDefaultVideoEncoders(MediaProfiles *profiles);
    437     static void createDefaultAudioEncoders(MediaProfiles *profiles);
    438     static void createDefaultVideoDecoders(MediaProfiles *profiles);
    439     static void createDefaultAudioDecoders(MediaProfiles *profiles);
    440     static void createDefaultEncoderOutputFileFormats(MediaProfiles *profiles);
    441     static void createDefaultImageEncodingQualityLevels(MediaProfiles *profiles);
    442     static void createDefaultImageDecodingMaxMemory(MediaProfiles *profiles);
    443     static void createDefaultVideoEditorCap(MediaProfiles *profiles);
    444     static void createDefaultExportVideoProfiles(MediaProfiles *profiles);
    445 
    446     static VideoEncoderCap* createDefaultH263VideoEncoderCap();
    447     static VideoEncoderCap* createDefaultM4vVideoEncoderCap();
    448     static AudioEncoderCap* createDefaultAmrNBEncoderCap();
    449 
    450     static int findTagForName(const NameToTagMap *map, size_t nMappings, const char *name);
    451 
    452     /**
    453      * Check on existing profiles with the following criteria:
    454      * 1. Low quality profile must have the lowest video
    455      *    resolution product (width x height)
    456      * 2. High quality profile must have the highest video
    457      *    resolution product (width x height)
    458      *
    459      * and add required low/high quality camcorder/timelapse
    460      * profiles if they are not found. This allows to remove
    461      * duplicate profile definitions in the media_profiles.xml
    462      * file.
    463      */
    464     void checkAndAddRequiredProfilesIfNecessary();
    465 
    466 
    467     // Mappings from name (for instance, codec name) to enum value
    468     static const NameToTagMap sVideoEncoderNameMap[];
    469     static const NameToTagMap sAudioEncoderNameMap[];
    470     static const NameToTagMap sFileFormatMap[];
    471     static const NameToTagMap sVideoDecoderNameMap[];
    472     static const NameToTagMap sAudioDecoderNameMap[];
    473     static const NameToTagMap sCamcorderQualityNameMap[];
    474 
    475     static bool sIsInitialized;
    476     static MediaProfiles *sInstance;
    477     static Mutex sLock;
    478     int mCurrentCameraId;
    479 
    480     Vector<CamcorderProfile*> mCamcorderProfiles;
    481     Vector<AudioEncoderCap*>  mAudioEncoders;
    482     Vector<VideoEncoderCap*>  mVideoEncoders;
    483     Vector<AudioDecoderCap*>  mAudioDecoders;
    484     Vector<VideoDecoderCap*>  mVideoDecoders;
    485     Vector<output_format>     mEncoderOutputFileFormats;
    486     Vector<ImageEncodingQualityLevels *>  mImageEncodingQualityLevels;
    487     KeyedVector<int, int> mStartTimeOffsets;
    488 
    489     typedef struct {
    490         bool mHasRefProfile;      // Refers to an existing profile
    491         int  mRefProfileIndex;    // Reference profile index
    492         int  mResolutionProduct;  // width x height
    493     } RequiredProfileRefInfo;     // Required low and high profiles
    494 
    495     typedef struct {
    496         RequiredProfileRefInfo mRefs[kNumRequiredProfiles];
    497         int mCameraId;
    498     } RequiredProfiles;
    499 
    500     RequiredProfiles *mRequiredProfileRefs;
    501     Vector<int>              mCameraIds;
    502     VideoEditorCap* mVideoEditorCap;
    503     Vector<ExportVideoProfile*> mVideoEditorExportProfiles;
    504 };
    505 
    506 }; // namespace android
    507 
    508 #endif // ANDROID_MEDIAPROFILES_H
    509 
    510