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