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