18253f44c3d0d8a7003dd2e6f728f9e3d63927727Steven Moreland/*
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_QVGA = 7,
36    CAMCORDER_QUALITY_2160P = 8,
37    CAMCORDER_QUALITY_LIST_END = 8,
38
39    CAMCORDER_QUALITY_TIME_LAPSE_LIST_START = 1000,
40    CAMCORDER_QUALITY_TIME_LAPSE_LOW  = 1000,
41    CAMCORDER_QUALITY_TIME_LAPSE_HIGH = 1001,
42    CAMCORDER_QUALITY_TIME_LAPSE_QCIF = 1002,
43    CAMCORDER_QUALITY_TIME_LAPSE_CIF = 1003,
44    CAMCORDER_QUALITY_TIME_LAPSE_480P = 1004,
45    CAMCORDER_QUALITY_TIME_LAPSE_720P = 1005,
46    CAMCORDER_QUALITY_TIME_LAPSE_1080P = 1006,
47    CAMCORDER_QUALITY_TIME_LAPSE_QVGA = 1007,
48    CAMCORDER_QUALITY_TIME_LAPSE_2160P = 1008,
49    CAMCORDER_QUALITY_TIME_LAPSE_LIST_END = 1008,
50
51    CAMCORDER_QUALITY_HIGH_SPEED_LIST_START = 2000,
52    CAMCORDER_QUALITY_HIGH_SPEED_LOW  = 2000,
53    CAMCORDER_QUALITY_HIGH_SPEED_HIGH = 2001,
54    CAMCORDER_QUALITY_HIGH_SPEED_480P = 2002,
55    CAMCORDER_QUALITY_HIGH_SPEED_720P = 2003,
56    CAMCORDER_QUALITY_HIGH_SPEED_1080P = 2004,
57    CAMCORDER_QUALITY_HIGH_SPEED_2160P = 2005,
58    CAMCORDER_QUALITY_HIGH_SPEED_LIST_END = 2005,
59};
60
61enum video_decoder {
62    VIDEO_DECODER_WMV,
63};
64
65enum audio_decoder {
66    AUDIO_DECODER_WMA,
67};
68
69
70class MediaProfiles
71{
72public:
73
74    /*
75     * If property media.settings.xml is not set:
76     *
77     * getInstance() will search through paths listed in xmlFiles.
78     * The search goes through members of xmlFiles in the order that they are
79     * defined, so files at lower indices have higher priority than those at
80     * higher indices.
81     *
82     * TODO: Add runtime validation of xml files. A search should be considered
83     * successful only when validation is successful.
84     */
85    static constexpr char const * const xmlFiles[] = {
86            "vendor/etc/media_profiles_V1_0.xml",
87            "system/etc/media_profiles.xml"
88            };
89
90    /**
91     * Returns the singleton instance for subsequence queries or NULL if error.
92     *
93     * If property media.settings.xml is set, getInstance() will attempt to read
94     * from file path in media.settings.xml. Otherwise, getInstance() will
95     * search through the list xmlFiles as described above.
96     *
97     * If the search is unsuccessful, the default instance will be created
98     * instead.
99     *
100     * TODO: After validation is added, getInstance() should handle validation
101     * failure properly.
102     */
103    static MediaProfiles* getInstance();
104
105    /**
106     * Returns the value for the given param name for the given camera at
107     * the given quality level, or -1 if error.
108     *
109     * Supported param name are:
110     * duration - the recording duration.
111     * file.format - output file format. see mediarecorder.h for details
112     * vid.codec - video encoder. see mediarecorder.h for details.
113     * aud.codec - audio encoder. see mediarecorder.h for details.
114     * vid.width - video frame width
115     * vid.height - video frame height
116     * vid.fps - video frame rate
117     * vid.bps - video bit rate
118     * aud.bps - audio bit rate
119     * aud.hz - audio sample rate
120     * aud.ch - number of audio channels
121     */
122    int getCamcorderProfileParamByName(const char *name, int cameraId,
123                                       camcorder_quality quality) const;
124
125    /**
126     * Returns true if a profile for the given camera at the given quality exists,
127     * or false if not.
128     */
129    bool hasCamcorderProfile(int cameraId, camcorder_quality quality) const;
130
131    /**
132     * Returns the output file formats supported.
133     */
134    Vector<output_format> getOutputFileFormats() const;
135
136    /**
137     * Returns the video encoders supported.
138     */
139    Vector<video_encoder> getVideoEncoders() const;
140
141    /**
142     * Returns the value for the given param name for the given video encoder
143     * returned from getVideoEncoderByIndex or -1 if error.
144     *
145     * Supported param name are:
146     * enc.vid.width.min - min video frame width
147     * enc.vid.width.max - max video frame width
148     * enc.vid.height.min - min video frame height
149     * enc.vid.height.max - max video frame height
150     * enc.vid.bps.min - min bit rate in bits per second
151     * enc.vid.bps.max - max bit rate in bits per second
152     * enc.vid.fps.min - min frame rate in frames per second
153     * enc.vid.fps.max - max frame rate in frames per second
154     */
155    int getVideoEncoderParamByName(const char *name, video_encoder 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
197private:
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() {}                               // 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
351    int getCamcorderProfileIndex(int cameraId, camcorder_quality quality) const;
352    void initRequiredProfileRefs(const Vector<int>& cameraIds);
353    int getRequiredProfileRefIndex(int cameraId);
354
355    // Debug
356    static void logVideoCodec(const VideoCodec& codec);
357    static void logAudioCodec(const AudioCodec& codec);
358    static void logVideoEncoderCap(const VideoEncoderCap& cap);
359    static void logAudioEncoderCap(const AudioEncoderCap& cap);
360    static void logVideoDecoderCap(const VideoDecoderCap& cap);
361    static void logAudioDecoderCap(const AudioDecoderCap& cap);
362
363    // Returns true if xmlFile exists.
364    // TODO: Add runtime validation.
365    static bool checkXmlFile(const char* xmlFile);
366
367    // If the xml configuration file does exist, use the settings
368    // from the xml
369    static MediaProfiles* createInstanceFromXmlFile(const char *xml);
370    static output_format createEncoderOutputFileFormat(const char **atts);
371    static VideoCodec* createVideoCodec(const char **atts, MediaProfiles *profiles);
372    static AudioCodec* createAudioCodec(const char **atts, MediaProfiles *profiles);
373    static AudioDecoderCap* createAudioDecoderCap(const char **atts);
374    static VideoDecoderCap* createVideoDecoderCap(const char **atts);
375    static VideoEncoderCap* createVideoEncoderCap(const char **atts);
376    static AudioEncoderCap* createAudioEncoderCap(const char **atts);
377
378    static CamcorderProfile* createCamcorderProfile(
379                int cameraId, const char **atts, Vector<int>& cameraIds);
380
381    static int getCameraId(const char **atts);
382
383    void addStartTimeOffset(int cameraId, const char **atts);
384
385    ImageEncodingQualityLevels* findImageEncodingQualityLevels(int cameraId) const;
386    void addImageEncodingQualityLevel(int cameraId, const char** atts);
387
388    // Customized element tag handler for parsing the xml configuration file.
389    static void startElementHandler(void *userData, const char *name, const char **atts);
390
391    // If the xml configuration file does not exist, use hard-coded values
392    static MediaProfiles* createDefaultInstance();
393
394    static CamcorderProfile *createDefaultCamcorderQcifProfile(camcorder_quality quality);
395    static CamcorderProfile *createDefaultCamcorderCifProfile(camcorder_quality quality);
396    static void createDefaultCamcorderLowProfiles(
397            MediaProfiles::CamcorderProfile **lowProfile,
398            MediaProfiles::CamcorderProfile **lowSpecificProfile);
399    static void createDefaultCamcorderHighProfiles(
400            MediaProfiles::CamcorderProfile **highProfile,
401            MediaProfiles::CamcorderProfile **highSpecificProfile);
402
403    static CamcorderProfile *createDefaultCamcorderTimeLapseQcifProfile(camcorder_quality quality);
404    static CamcorderProfile *createDefaultCamcorderTimeLapse480pProfile(camcorder_quality quality);
405    static void createDefaultCamcorderTimeLapseLowProfiles(
406            MediaProfiles::CamcorderProfile **lowTimeLapseProfile,
407            MediaProfiles::CamcorderProfile **lowSpecificTimeLapseProfile);
408    static void createDefaultCamcorderTimeLapseHighProfiles(
409            MediaProfiles::CamcorderProfile **highTimeLapseProfile,
410            MediaProfiles::CamcorderProfile **highSpecificTimeLapseProfile);
411
412    static void createDefaultCamcorderProfiles(MediaProfiles *profiles);
413    static void createDefaultVideoEncoders(MediaProfiles *profiles);
414    static void createDefaultAudioEncoders(MediaProfiles *profiles);
415    static void createDefaultVideoDecoders(MediaProfiles *profiles);
416    static void createDefaultAudioDecoders(MediaProfiles *profiles);
417    static void createDefaultEncoderOutputFileFormats(MediaProfiles *profiles);
418    static void createDefaultImageEncodingQualityLevels(MediaProfiles *profiles);
419    static void createDefaultImageDecodingMaxMemory(MediaProfiles *profiles);
420
421    static VideoEncoderCap* createDefaultH263VideoEncoderCap();
422    static VideoEncoderCap* createDefaultM4vVideoEncoderCap();
423    static AudioEncoderCap* createDefaultAmrNBEncoderCap();
424
425    static int findTagForName(const NameToTagMap *map, size_t nMappings, const char *name);
426
427    /**
428     * Check on existing profiles with the following criteria:
429     * 1. Low quality profile must have the lowest video
430     *    resolution product (width x height)
431     * 2. High quality profile must have the highest video
432     *    resolution product (width x height)
433     *
434     * and add required low/high quality camcorder/timelapse
435     * profiles if they are not found. This allows to remove
436     * duplicate profile definitions in the media_profiles.xml
437     * file.
438     */
439    void checkAndAddRequiredProfilesIfNecessary();
440
441
442    // Mappings from name (for instance, codec name) to enum value
443    static const NameToTagMap sVideoEncoderNameMap[];
444    static const NameToTagMap sAudioEncoderNameMap[];
445    static const NameToTagMap sFileFormatMap[];
446    static const NameToTagMap sVideoDecoderNameMap[];
447    static const NameToTagMap sAudioDecoderNameMap[];
448    static const NameToTagMap sCamcorderQualityNameMap[];
449
450    static bool sIsInitialized;
451    static MediaProfiles *sInstance;
452    static Mutex sLock;
453    int mCurrentCameraId;
454
455    Vector<CamcorderProfile*> mCamcorderProfiles;
456    Vector<AudioEncoderCap*>  mAudioEncoders;
457    Vector<VideoEncoderCap*>  mVideoEncoders;
458    Vector<AudioDecoderCap*>  mAudioDecoders;
459    Vector<VideoDecoderCap*>  mVideoDecoders;
460    Vector<output_format>     mEncoderOutputFileFormats;
461    Vector<ImageEncodingQualityLevels *>  mImageEncodingQualityLevels;
462    KeyedVector<int, int> mStartTimeOffsets;
463
464    typedef struct {
465        bool mHasRefProfile;      // Refers to an existing profile
466        int  mRefProfileIndex;    // Reference profile index
467        int  mResolutionProduct;  // width x height
468    } RequiredProfileRefInfo;     // Required low and high profiles
469
470    typedef struct {
471        RequiredProfileRefInfo mRefs[kNumRequiredProfiles];
472        int mCameraId;
473    } RequiredProfiles;
474
475    RequiredProfiles *mRequiredProfileRefs;
476    Vector<int>              mCameraIds;
477};
478
479}; // namespace android
480
481#endif // ANDROID_MEDIAPROFILES_H
482