James Dong | c371194 | 2010-01-19 17:45:38 -0800 | [diff] [blame] | 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_HIGH = 0, |
| 28 | CAMCORDER_QUALITY_LOW = 1 |
| 29 | }; |
| 30 | |
| 31 | enum video_decoder { |
| 32 | VIDEO_DECODER_WMV, |
| 33 | }; |
| 34 | |
| 35 | enum audio_decoder { |
| 36 | AUDIO_DECODER_WMA, |
| 37 | }; |
| 38 | |
| 39 | |
| 40 | class MediaProfiles |
| 41 | { |
| 42 | public: |
| 43 | |
| 44 | /** |
| 45 | * Returns the singleton instance for subsequence queries. |
| 46 | * or NULL if error. |
| 47 | */ |
| 48 | static MediaProfiles* getInstance(); |
| 49 | |
| 50 | /** |
| 51 | * Returns the value for the given param name at the given quality level, |
| 52 | * or -1 if error. |
| 53 | * |
| 54 | * Supported param name are: |
| 55 | * file.format - output file format. see mediarecorder.h for details |
| 56 | * codec.vid - video encoder. see mediarecorder.h for details. |
| 57 | * codec.aud - audio encoder. see mediarecorder.h for details. |
| 58 | * vid.width - video frame width |
| 59 | * vid.height - video frame height |
| 60 | * vid.fps - video frame rate |
| 61 | * vid.bps - video bit rate |
| 62 | * aud.bps - audio bit rate |
| 63 | * aud.hz - audio sample rate |
| 64 | * aud.ch - number of audio channels |
| 65 | */ |
| 66 | int getCamcorderProfileParamByName(const char *name, camcorder_quality quality) const; |
| 67 | |
| 68 | /** |
| 69 | * Returns the output file formats supported. |
| 70 | */ |
| 71 | Vector<output_format> getOutputFileFormats() const; |
| 72 | |
| 73 | /** |
| 74 | * Returns the video encoders supported. |
| 75 | */ |
| 76 | Vector<video_encoder> getVideoEncoders() const; |
| 77 | |
| 78 | /** |
| 79 | * Returns the value for the given param name for the given video encoder |
| 80 | * returned from getVideoEncoderByIndex or -1 if error. |
| 81 | * |
| 82 | * Supported param name are: |
| 83 | * enc.vid.width.min - min video frame width |
| 84 | * enc.vid.width.max - max video frame width |
| 85 | * enc.vid.height.min - min video frame height |
| 86 | * enc.vid.height.max - max video frame height |
| 87 | * enc.vid.bps.min - min bit rate in bits per second |
| 88 | * enc.vid.bps.max - max bit rate in bits per second |
| 89 | * enc.vid.fps.min - min frame rate in frames per second |
| 90 | * enc.vid.fps.max - max frame rate in frames per second |
| 91 | */ |
| 92 | int getVideoEncoderParamByName(const char *name, video_encoder codec) const; |
| 93 | |
| 94 | /** |
| 95 | * Returns the audio encoders supported. |
| 96 | */ |
| 97 | Vector<audio_encoder> getAudioEncoders() const; |
| 98 | |
| 99 | /** |
| 100 | * Returns the value for the given param name for the given audio encoder |
| 101 | * returned from getAudioEncoderByIndex or -1 if error. |
| 102 | * |
| 103 | * Supported param name are: |
| 104 | * enc.aud.ch.min - min number of channels |
| 105 | * enc.aud.ch.max - max number of channels |
| 106 | * enc.aud.bps.min - min bit rate in bits per second |
| 107 | * enc.aud.bps.max - max bit rate in bits per second |
| 108 | * enc.aud.hz.min - min sample rate in samples per second |
| 109 | * enc.aud.hz.max - max sample rate in samples per second |
| 110 | */ |
| 111 | int getAudioEncoderParamByName(const char *name, audio_encoder codec) const; |
| 112 | |
| 113 | /** |
| 114 | * Returns the video decoders supported. |
| 115 | */ |
| 116 | Vector<video_decoder> getVideoDecoders() const; |
| 117 | |
| 118 | /** |
| 119 | * Returns the audio decoders supported. |
| 120 | */ |
| 121 | Vector<audio_decoder> getAudioDecoders() const; |
| 122 | |
| 123 | private: |
| 124 | MediaProfiles& operator=(const MediaProfiles&); // Don't call me |
| 125 | MediaProfiles(const MediaProfiles&); // Don't call me |
| 126 | MediaProfiles() {} // Dummy default constructor |
| 127 | ~MediaProfiles(); // Don't delete me |
| 128 | |
| 129 | struct VideoCodec { |
| 130 | VideoCodec(video_encoder codec, int bitRate, int frameWidth, int frameHeight, int frameRate) |
| 131 | : mCodec(codec), |
| 132 | mBitRate(bitRate), |
| 133 | mFrameWidth(frameWidth), |
| 134 | mFrameHeight(frameHeight), |
| 135 | mFrameRate(frameRate) {} |
| 136 | |
| 137 | ~VideoCodec() {} |
| 138 | |
| 139 | video_encoder mCodec; |
| 140 | int mBitRate; |
| 141 | int mFrameWidth; |
| 142 | int mFrameHeight; |
| 143 | int mFrameRate; |
| 144 | }; |
| 145 | |
| 146 | struct AudioCodec { |
| 147 | AudioCodec(audio_encoder codec, int bitRate, int sampleRate, int channels) |
| 148 | : mCodec(codec), |
| 149 | mBitRate(bitRate), |
| 150 | mSampleRate(sampleRate), |
| 151 | mChannels(channels) {} |
| 152 | |
| 153 | ~AudioCodec() {} |
| 154 | |
| 155 | audio_encoder mCodec; |
| 156 | int mBitRate; |
| 157 | int mSampleRate; |
| 158 | int mChannels; |
| 159 | }; |
| 160 | |
| 161 | struct CamcorderProfile { |
| 162 | CamcorderProfile() |
| 163 | : mFileFormat(OUTPUT_FORMAT_THREE_GPP), |
| 164 | mQuality(CAMCORDER_QUALITY_HIGH), |
| 165 | mDuration(0), |
| 166 | mVideoCodec(0), |
| 167 | mAudioCodec(0) {} |
| 168 | |
| 169 | ~CamcorderProfile() { |
| 170 | delete mVideoCodec; |
| 171 | delete mAudioCodec; |
| 172 | } |
| 173 | |
| 174 | output_format mFileFormat; |
| 175 | camcorder_quality mQuality; |
| 176 | int mDuration; |
| 177 | VideoCodec *mVideoCodec; |
| 178 | AudioCodec *mAudioCodec; |
| 179 | }; |
| 180 | |
| 181 | struct VideoEncoderCap { |
| 182 | // Ugly constructor |
| 183 | VideoEncoderCap(video_encoder codec, |
| 184 | int minBitRate, int maxBitRate, |
| 185 | int minFrameWidth, int maxFrameWidth, |
| 186 | int minFrameHeight, int maxFrameHeight, |
| 187 | int minFrameRate, int maxFrameRate) |
| 188 | : mCodec(codec), |
| 189 | mMinBitRate(minBitRate), mMaxBitRate(maxBitRate), |
| 190 | mMinFrameWidth(minFrameWidth), mMaxFrameWidth(maxFrameWidth), |
| 191 | mMinFrameHeight(minFrameHeight), mMaxFrameHeight(maxFrameHeight), |
| 192 | mMinFrameRate(minFrameRate), mMaxFrameRate(maxFrameRate) {} |
| 193 | |
| 194 | ~VideoEncoderCap() {} |
| 195 | |
| 196 | video_encoder mCodec; |
| 197 | int mMinBitRate, mMaxBitRate; |
| 198 | int mMinFrameWidth, mMaxFrameWidth; |
| 199 | int mMinFrameHeight, mMaxFrameHeight; |
| 200 | int mMinFrameRate, mMaxFrameRate; |
| 201 | }; |
| 202 | |
| 203 | struct AudioEncoderCap { |
| 204 | // Ugly constructor |
| 205 | AudioEncoderCap(audio_encoder codec, |
| 206 | int minBitRate, int maxBitRate, |
| 207 | int minSampleRate, int maxSampleRate, |
| 208 | int minChannels, int maxChannels) |
| 209 | : mCodec(codec), |
| 210 | mMinBitRate(minBitRate), mMaxBitRate(maxBitRate), |
| 211 | mMinSampleRate(minSampleRate), mMaxSampleRate(maxSampleRate), |
| 212 | mMinChannels(minChannels), mMaxChannels(maxChannels) {} |
| 213 | |
| 214 | ~AudioEncoderCap() {} |
| 215 | |
| 216 | audio_encoder mCodec; |
| 217 | int mMinBitRate, mMaxBitRate; |
| 218 | int mMinSampleRate, mMaxSampleRate; |
| 219 | int mMinChannels, mMaxChannels; |
| 220 | }; |
| 221 | |
| 222 | struct VideoDecoderCap { |
| 223 | VideoDecoderCap(video_decoder codec): mCodec(codec) {} |
| 224 | ~VideoDecoderCap() {} |
| 225 | |
| 226 | video_decoder mCodec; |
| 227 | }; |
| 228 | |
| 229 | struct AudioDecoderCap { |
| 230 | AudioDecoderCap(audio_decoder codec): mCodec(codec) {} |
| 231 | ~AudioDecoderCap() {} |
| 232 | |
| 233 | audio_decoder mCodec; |
| 234 | }; |
| 235 | |
| 236 | struct NameToTagMap { |
| 237 | const char* name; |
| 238 | int tag; |
| 239 | }; |
| 240 | |
| 241 | // Debug |
| 242 | static void logVideoCodec(const VideoCodec& codec); |
| 243 | static void logAudioCodec(const AudioCodec& codec); |
| 244 | static void logVideoEncoderCap(const VideoEncoderCap& cap); |
| 245 | static void logAudioEncoderCap(const AudioEncoderCap& cap); |
| 246 | static void logVideoDecoderCap(const VideoDecoderCap& cap); |
| 247 | static void logAudioDecoderCap(const AudioDecoderCap& cap); |
| 248 | |
| 249 | // If the xml configuration file does exist, use the settings |
| 250 | // from the xml |
| 251 | static MediaProfiles* createInstanceFromXmlFile(const char *xml); |
| 252 | static output_format createEncoderOutputFileFormat(const char **atts); |
| 253 | static VideoCodec* createVideoCodec(const char **atts, MediaProfiles *profiles); |
| 254 | static AudioCodec* createAudioCodec(const char **atts, MediaProfiles *profiles); |
| 255 | static AudioDecoderCap* createAudioDecoderCap(const char **atts); |
| 256 | static VideoDecoderCap* createVideoDecoderCap(const char **atts); |
| 257 | static VideoEncoderCap* createVideoEncoderCap(const char **atts); |
| 258 | static AudioEncoderCap* createAudioEncoderCap(const char **atts); |
| 259 | static CamcorderProfile* createCamcorderProfile(const char **atts); |
| 260 | |
| 261 | // Customized element tag handler for parsing the xml configuration file. |
| 262 | static void startElementHandler(void *userData, const char *name, const char **atts); |
| 263 | |
| 264 | // If the xml configuration file does not exist, use hard-coded values |
| 265 | static MediaProfiles* createDefaultInstance(); |
| 266 | static CamcorderProfile *createDefaultCamcorderLowProfile(); |
| 267 | static CamcorderProfile *createDefaultCamcorderHighProfile(); |
| 268 | static void createDefaultCamcorderProfiles(MediaProfiles *profiles); |
| 269 | static void createDefaultVideoEncoders(MediaProfiles *profiles); |
| 270 | static void createDefaultAudioEncoders(MediaProfiles *profiles); |
| 271 | static void createDefaultVideoDecoders(MediaProfiles *profiles); |
| 272 | static void createDefaultAudioDecoders(MediaProfiles *profiles); |
| 273 | static void createDefaultEncoderOutputFileFormats(MediaProfiles *profiles); |
| 274 | static VideoEncoderCap* createDefaultH263VideoEncoderCap(); |
| 275 | static VideoEncoderCap* createDefaultM4vVideoEncoderCap(); |
| 276 | static AudioEncoderCap* createDefaultAmrNBEncoderCap(); |
| 277 | |
| 278 | static int findTagForName(const NameToTagMap *map, size_t nMappings, const char *name); |
| 279 | |
| 280 | // Mappings from name (for instance, codec name) to enum value |
| 281 | static const NameToTagMap sVideoEncoderNameMap[]; |
| 282 | static const NameToTagMap sAudioEncoderNameMap[]; |
| 283 | static const NameToTagMap sFileFormatMap[]; |
| 284 | static const NameToTagMap sVideoDecoderNameMap[]; |
| 285 | static const NameToTagMap sAudioDecoderNameMap[]; |
| 286 | static const NameToTagMap sCamcorderQualityNameMap[]; |
| 287 | |
| 288 | static bool sIsInitialized; |
| 289 | static MediaProfiles *sInstance; |
| 290 | static Mutex sLock; |
| 291 | |
| 292 | Vector<CamcorderProfile*> mCamcorderProfiles; |
| 293 | Vector<AudioEncoderCap*> mAudioEncoders; |
| 294 | Vector<VideoEncoderCap*> mVideoEncoders; |
| 295 | Vector<AudioDecoderCap*> mAudioDecoders; |
| 296 | Vector<VideoDecoderCap*> mVideoDecoders; |
| 297 | Vector<output_format> mEncoderOutputFileFormats; |
| 298 | }; |
| 299 | |
| 300 | }; // namespace android |
| 301 | |
| 302 | #endif // ANDROID_MEDIAPROFILES_H |
| 303 | |