MediaProfiles.h revision 0f056290cb16763453f18bbef80cde673041dbbc
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
24namespace android {
25
26enum 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_LIST_END = 6,
36
37    CAMCORDER_QUALITY_TIME_LAPSE_LIST_START = 1000,
38    CAMCORDER_QUALITY_TIME_LAPSE_LOW  = 1000,
39    CAMCORDER_QUALITY_TIME_LAPSE_HIGH = 1001,
40    CAMCORDER_QUALITY_TIME_LAPSE_QCIF = 1002,
41    CAMCORDER_QUALITY_TIME_LAPSE_CIF = 1003,
42    CAMCORDER_QUALITY_TIME_LAPSE_480P = 1004,
43    CAMCORDER_QUALITY_TIME_LAPSE_720P = 1005,
44    CAMCORDER_QUALITY_TIME_LAPSE_1080P = 1006,
45    CAMCORDER_QUALITY_TIME_LAPSE_LIST_END = 1006,
46};
47
48enum video_decoder {
49    VIDEO_DECODER_WMV,
50};
51
52enum audio_decoder {
53    AUDIO_DECODER_WMA,
54};
55
56
57class MediaProfiles
58{
59public:
60
61    /**
62     * Returns the singleton instance for subsequence queries.
63     * or NULL if error.
64     */
65    static MediaProfiles* getInstance();
66
67    /**
68     * Returns the value for the given param name for the given camera at
69     * the given quality level, or -1 if error.
70     *
71     * Supported param name are:
72     * duration - the recording duration.
73     * file.format - output file format. see mediarecorder.h for details
74     * vid.codec - video encoder. see mediarecorder.h for details.
75     * aud.codec - audio encoder. see mediarecorder.h for details.
76     * vid.width - video frame width
77     * vid.height - video frame height
78     * vid.fps - video frame rate
79     * vid.bps - video bit rate
80     * aud.bps - audio bit rate
81     * aud.hz - audio sample rate
82     * aud.ch - number of audio channels
83     */
84    int getCamcorderProfileParamByName(const char *name, int cameraId,
85                                       camcorder_quality quality) const;
86
87    /**
88     * Returns true if a profile for the given camera at the given quality exists,
89     * or false if not.
90     */
91    bool hasCamcorderProfile(int cameraId, camcorder_quality quality) const;
92
93    /**
94     * Returns the output file formats supported.
95     */
96    Vector<output_format> getOutputFileFormats() const;
97
98    /**
99     * Returns the video encoders supported.
100     */
101    Vector<video_encoder> getVideoEncoders() const;
102
103    /**
104     * Returns the value for the given param name for the given video encoder
105     * returned from getVideoEncoderByIndex or -1 if error.
106     *
107     * Supported param name are:
108     * enc.vid.width.min - min video frame width
109     * enc.vid.width.max - max video frame width
110     * enc.vid.height.min - min video frame height
111     * enc.vid.height.max - max video frame height
112     * enc.vid.bps.min - min bit rate in bits per second
113     * enc.vid.bps.max - max bit rate in bits per second
114     * enc.vid.fps.min - min frame rate in frames per second
115     * enc.vid.fps.max - max frame rate in frames per second
116     */
117    int getVideoEncoderParamByName(const char *name, video_encoder codec) const;
118
119    /**
120     * Returns the audio encoders supported.
121     */
122    Vector<audio_encoder> getAudioEncoders() const;
123
124    /**
125     * Returns the value for the given param name for the given audio encoder
126     * returned from getAudioEncoderByIndex or -1 if error.
127     *
128     * Supported param name are:
129     * enc.aud.ch.min - min number of channels
130     * enc.aud.ch.max - max number of channels
131     * enc.aud.bps.min - min bit rate in bits per second
132     * enc.aud.bps.max - max bit rate in bits per second
133     * enc.aud.hz.min - min sample rate in samples per second
134     * enc.aud.hz.max - max sample rate in samples per second
135     */
136    int getAudioEncoderParamByName(const char *name, audio_encoder codec) const;
137
138    /**
139      * Returns the video decoders supported.
140      */
141    Vector<video_decoder> getVideoDecoders() const;
142
143     /**
144      * Returns the audio decoders supported.
145      */
146    Vector<audio_decoder> getAudioDecoders() const;
147
148    /**
149     * Returns the number of image encoding quality levels supported.
150     */
151    Vector<int> getImageEncodingQualityLevels(int cameraId) const;
152
153    /**
154     * Returns the start time offset (in ms) for the given camera Id.
155     * If the given camera Id does not exist, -1 will be returned.
156     */
157    int getStartTimeOffsetMs(int cameraId) const;
158
159private:
160    enum {
161        // Camcorder profiles (high/low) and timelapse profiles (high/low)
162        kNumRequiredProfiles = 4,
163    };
164
165    MediaProfiles& operator=(const MediaProfiles&);  // Don't call me
166    MediaProfiles(const MediaProfiles&);             // Don't call me
167    MediaProfiles() {}                               // Dummy default constructor
168    ~MediaProfiles();                                // Don't delete me
169
170    struct VideoCodec {
171        VideoCodec(video_encoder codec, int bitRate, int frameWidth, int frameHeight, int frameRate)
172            : mCodec(codec),
173              mBitRate(bitRate),
174              mFrameWidth(frameWidth),
175              mFrameHeight(frameHeight),
176              mFrameRate(frameRate) {}
177
178        VideoCodec(const VideoCodec& copy) {
179            mCodec = copy.mCodec;
180            mBitRate = copy.mBitRate;
181            mFrameWidth = copy.mFrameWidth;
182            mFrameHeight = copy.mFrameHeight;
183            mFrameRate = copy.mFrameRate;
184        }
185
186        ~VideoCodec() {}
187
188        video_encoder mCodec;
189        int mBitRate;
190        int mFrameWidth;
191        int mFrameHeight;
192        int mFrameRate;
193    };
194
195    struct AudioCodec {
196        AudioCodec(audio_encoder codec, int bitRate, int sampleRate, int channels)
197            : mCodec(codec),
198              mBitRate(bitRate),
199              mSampleRate(sampleRate),
200              mChannels(channels) {}
201
202        AudioCodec(const AudioCodec& copy) {
203            mCodec = copy.mCodec;
204            mBitRate = copy.mBitRate;
205            mSampleRate = copy.mSampleRate;
206            mChannels = copy.mChannels;
207        }
208
209        ~AudioCodec() {}
210
211        audio_encoder mCodec;
212        int mBitRate;
213        int mSampleRate;
214        int mChannels;
215    };
216
217    struct CamcorderProfile {
218        CamcorderProfile()
219            : mCameraId(0),
220              mFileFormat(OUTPUT_FORMAT_THREE_GPP),
221              mQuality(CAMCORDER_QUALITY_HIGH),
222              mDuration(0),
223              mVideoCodec(0),
224              mAudioCodec(0) {}
225
226        CamcorderProfile(const CamcorderProfile& copy) {
227            mCameraId = copy.mCameraId;
228            mFileFormat = copy.mFileFormat;
229            mQuality = copy.mQuality;
230            mDuration = copy.mDuration;
231            mVideoCodec = new VideoCodec(*copy.mVideoCodec);
232            mAudioCodec = new AudioCodec(*copy.mAudioCodec);
233        }
234
235        ~CamcorderProfile() {
236            delete mVideoCodec;
237            delete mAudioCodec;
238        }
239
240        int mCameraId;
241        output_format mFileFormat;
242        camcorder_quality mQuality;
243        int mDuration;
244        VideoCodec *mVideoCodec;
245        AudioCodec *mAudioCodec;
246    };
247
248    struct VideoEncoderCap {
249        // Ugly constructor
250        VideoEncoderCap(video_encoder codec,
251                        int minBitRate, int maxBitRate,
252                        int minFrameWidth, int maxFrameWidth,
253                        int minFrameHeight, int maxFrameHeight,
254                        int minFrameRate, int maxFrameRate)
255            : mCodec(codec),
256              mMinBitRate(minBitRate), mMaxBitRate(maxBitRate),
257              mMinFrameWidth(minFrameWidth), mMaxFrameWidth(maxFrameWidth),
258              mMinFrameHeight(minFrameHeight), mMaxFrameHeight(maxFrameHeight),
259              mMinFrameRate(minFrameRate), mMaxFrameRate(maxFrameRate) {}
260
261         ~VideoEncoderCap() {}
262
263        video_encoder mCodec;
264        int mMinBitRate, mMaxBitRate;
265        int mMinFrameWidth, mMaxFrameWidth;
266        int mMinFrameHeight, mMaxFrameHeight;
267        int mMinFrameRate, mMaxFrameRate;
268    };
269
270    struct AudioEncoderCap {
271        // Ugly constructor
272        AudioEncoderCap(audio_encoder codec,
273                        int minBitRate, int maxBitRate,
274                        int minSampleRate, int maxSampleRate,
275                        int minChannels, int maxChannels)
276            : mCodec(codec),
277              mMinBitRate(minBitRate), mMaxBitRate(maxBitRate),
278              mMinSampleRate(minSampleRate), mMaxSampleRate(maxSampleRate),
279              mMinChannels(minChannels), mMaxChannels(maxChannels) {}
280
281        ~AudioEncoderCap() {}
282
283        audio_encoder mCodec;
284        int mMinBitRate, mMaxBitRate;
285        int mMinSampleRate, mMaxSampleRate;
286        int mMinChannels, mMaxChannels;
287    };
288
289    struct VideoDecoderCap {
290        VideoDecoderCap(video_decoder codec): mCodec(codec) {}
291        ~VideoDecoderCap() {}
292
293        video_decoder mCodec;
294    };
295
296    struct AudioDecoderCap {
297        AudioDecoderCap(audio_decoder codec): mCodec(codec) {}
298        ~AudioDecoderCap() {}
299
300        audio_decoder mCodec;
301    };
302
303    struct NameToTagMap {
304        const char* name;
305        int tag;
306    };
307
308    struct ImageEncodingQualityLevels {
309        int mCameraId;
310        Vector<int> mLevels;
311    };
312
313    int getCamcorderProfileIndex(int cameraId, camcorder_quality quality) const;
314    void initRequiredProfileRefs(const Vector<int>& cameraIds);
315    int getRequiredProfileRefIndex(int cameraId);
316
317    // Debug
318    static void logVideoCodec(const VideoCodec& codec);
319    static void logAudioCodec(const AudioCodec& codec);
320    static void logVideoEncoderCap(const VideoEncoderCap& cap);
321    static void logAudioEncoderCap(const AudioEncoderCap& cap);
322    static void logVideoDecoderCap(const VideoDecoderCap& cap);
323    static void logAudioDecoderCap(const AudioDecoderCap& cap);
324
325    // If the xml configuration file does exist, use the settings
326    // from the xml
327    static MediaProfiles* createInstanceFromXmlFile(const char *xml);
328    static output_format createEncoderOutputFileFormat(const char **atts);
329    static VideoCodec* createVideoCodec(const char **atts, MediaProfiles *profiles);
330    static AudioCodec* createAudioCodec(const char **atts, MediaProfiles *profiles);
331    static AudioDecoderCap* createAudioDecoderCap(const char **atts);
332    static VideoDecoderCap* createVideoDecoderCap(const char **atts);
333    static VideoEncoderCap* createVideoEncoderCap(const char **atts);
334    static AudioEncoderCap* createAudioEncoderCap(const char **atts);
335
336    static CamcorderProfile* createCamcorderProfile(
337                int cameraId, const char **atts, Vector<int>& cameraIds);
338
339    static int getCameraId(const char **atts);
340
341    void addStartTimeOffset(int cameraId, const char **atts);
342
343    ImageEncodingQualityLevels* findImageEncodingQualityLevels(int cameraId) const;
344    void addImageEncodingQualityLevel(int cameraId, const char** atts);
345
346    // Customized element tag handler for parsing the xml configuration file.
347    static void startElementHandler(void *userData, const char *name, const char **atts);
348
349    // If the xml configuration file does not exist, use hard-coded values
350    static MediaProfiles* createDefaultInstance();
351
352    static CamcorderProfile *createDefaultCamcorderQcifProfile(camcorder_quality quality);
353    static CamcorderProfile *createDefaultCamcorderCifProfile(camcorder_quality quality);
354    static void createDefaultCamcorderLowProfiles(
355            MediaProfiles::CamcorderProfile **lowProfile,
356            MediaProfiles::CamcorderProfile **lowSpecificProfile);
357    static void createDefaultCamcorderHighProfiles(
358            MediaProfiles::CamcorderProfile **highProfile,
359            MediaProfiles::CamcorderProfile **highSpecificProfile);
360
361    static CamcorderProfile *createDefaultCamcorderTimeLapseQcifProfile(camcorder_quality quality);
362    static CamcorderProfile *createDefaultCamcorderTimeLapse480pProfile(camcorder_quality quality);
363    static void createDefaultCamcorderTimeLapseLowProfiles(
364            MediaProfiles::CamcorderProfile **lowTimeLapseProfile,
365            MediaProfiles::CamcorderProfile **lowSpecificTimeLapseProfile);
366    static void createDefaultCamcorderTimeLapseHighProfiles(
367            MediaProfiles::CamcorderProfile **highTimeLapseProfile,
368            MediaProfiles::CamcorderProfile **highSpecificTimeLapseProfile);
369
370    static void createDefaultCamcorderProfiles(MediaProfiles *profiles);
371    static void createDefaultVideoEncoders(MediaProfiles *profiles);
372    static void createDefaultAudioEncoders(MediaProfiles *profiles);
373    static void createDefaultVideoDecoders(MediaProfiles *profiles);
374    static void createDefaultAudioDecoders(MediaProfiles *profiles);
375    static void createDefaultEncoderOutputFileFormats(MediaProfiles *profiles);
376    static void createDefaultImageEncodingQualityLevels(MediaProfiles *profiles);
377    static void createDefaultImageDecodingMaxMemory(MediaProfiles *profiles);
378    static VideoEncoderCap* createDefaultH263VideoEncoderCap();
379    static VideoEncoderCap* createDefaultM4vVideoEncoderCap();
380    static AudioEncoderCap* createDefaultAmrNBEncoderCap();
381
382    static int findTagForName(const NameToTagMap *map, size_t nMappings, const char *name);
383
384    /**
385     * Check on existing profiles with the following criteria:
386     * 1. Low quality profile must have the lowest video
387     *    resolution product (width x height)
388     * 2. High quality profile must have the highest video
389     *    resolution product (width x height)
390     *
391     * and add required low/high quality camcorder/timelapse
392     * profiles if they are not found. This allows to remove
393     * duplicate profile definitions in the media_profiles.xml
394     * file.
395     */
396    void checkAndAddRequiredProfilesIfNecessary();
397
398
399    // Mappings from name (for instance, codec name) to enum value
400    static const NameToTagMap sVideoEncoderNameMap[];
401    static const NameToTagMap sAudioEncoderNameMap[];
402    static const NameToTagMap sFileFormatMap[];
403    static const NameToTagMap sVideoDecoderNameMap[];
404    static const NameToTagMap sAudioDecoderNameMap[];
405    static const NameToTagMap sCamcorderQualityNameMap[];
406
407    static bool sIsInitialized;
408    static MediaProfiles *sInstance;
409    static Mutex sLock;
410    int mCurrentCameraId;
411
412    Vector<CamcorderProfile*> mCamcorderProfiles;
413    Vector<AudioEncoderCap*>  mAudioEncoders;
414    Vector<VideoEncoderCap*>  mVideoEncoders;
415    Vector<AudioDecoderCap*>  mAudioDecoders;
416    Vector<VideoDecoderCap*>  mVideoDecoders;
417    Vector<output_format>     mEncoderOutputFileFormats;
418    Vector<ImageEncodingQualityLevels *>  mImageEncodingQualityLevels;
419    KeyedVector<int, int> mStartTimeOffsets;
420
421    typedef struct {
422        bool mHasRefProfile;      // Refers to an existing profile
423        int  mRefProfileIndex;    // Reference profile index
424        int  mResolutionProduct;  // width x height
425    } RequiredProfileRefInfo;     // Required low and high profiles
426
427    typedef struct {
428        RequiredProfileRefInfo mRefs[kNumRequiredProfiles];
429        int mCameraId;
430    } RequiredProfiles;
431
432    RequiredProfiles *mRequiredProfileRefs;
433    Vector<int>              mCameraIds;
434};
435
436}; // namespace android
437
438#endif // ANDROID_MEDIAPROFILES_H
439
440