Home | History | Annotate | Download | only in libmedia
      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 
     19 //#define LOG_NDEBUG 0
     20 #define LOG_TAG "MediaProfiles"
     21 
     22 #include <stdlib.h>
     23 #include <utils/Log.h>
     24 #include <utils/Vector.h>
     25 #include <cutils/properties.h>
     26 #include <expat.h>
     27 #include <media/MediaProfiles.h>
     28 #include <media/stagefright/MediaDebug.h>
     29 
     30 namespace android {
     31 
     32 Mutex MediaProfiles::sLock;
     33 bool MediaProfiles::sIsInitialized = false;
     34 MediaProfiles *MediaProfiles::sInstance = NULL;
     35 
     36 const MediaProfiles::NameToTagMap MediaProfiles::sVideoEncoderNameMap[] = {
     37     {"h263", VIDEO_ENCODER_H263},
     38     {"h264", VIDEO_ENCODER_H264},
     39     {"m4v",  VIDEO_ENCODER_MPEG_4_SP}
     40 };
     41 
     42 const MediaProfiles::NameToTagMap MediaProfiles::sAudioEncoderNameMap[] = {
     43     {"amrnb", AUDIO_ENCODER_AMR_NB},
     44     {"amrwb", AUDIO_ENCODER_AMR_WB},
     45     {"aac",   AUDIO_ENCODER_AAC},
     46 };
     47 
     48 const MediaProfiles::NameToTagMap MediaProfiles::sFileFormatMap[] = {
     49     {"3gp", OUTPUT_FORMAT_THREE_GPP},
     50     {"mp4", OUTPUT_FORMAT_MPEG_4}
     51 };
     52 
     53 const MediaProfiles::NameToTagMap MediaProfiles::sVideoDecoderNameMap[] = {
     54     {"wmv", VIDEO_DECODER_WMV}
     55 };
     56 
     57 const MediaProfiles::NameToTagMap MediaProfiles::sAudioDecoderNameMap[] = {
     58     {"wma", AUDIO_DECODER_WMA}
     59 };
     60 
     61 const MediaProfiles::NameToTagMap MediaProfiles::sCamcorderQualityNameMap[] = {
     62     {"high", CAMCORDER_QUALITY_HIGH},
     63     {"low",  CAMCORDER_QUALITY_LOW}
     64 };
     65 
     66 /*static*/ void
     67 MediaProfiles::logVideoCodec(const MediaProfiles::VideoCodec& codec)
     68 {
     69     LOGV("video codec:");
     70     LOGV("codec = %d", codec.mCodec);
     71     LOGV("bit rate: %d", codec.mBitRate);
     72     LOGV("frame width: %d", codec.mFrameWidth);
     73     LOGV("frame height: %d", codec.mFrameHeight);
     74     LOGV("frame rate: %d", codec.mFrameRate);
     75 }
     76 
     77 /*static*/ void
     78 MediaProfiles::logAudioCodec(const MediaProfiles::AudioCodec& codec)
     79 {
     80     LOGV("audio codec:");
     81     LOGV("codec = %d", codec.mCodec);
     82     LOGV("bit rate: %d", codec.mBitRate);
     83     LOGV("sample rate: %d", codec.mSampleRate);
     84     LOGV("number of channels: %d", codec.mChannels);
     85 }
     86 
     87 /*static*/ void
     88 MediaProfiles::logVideoEncoderCap(const MediaProfiles::VideoEncoderCap& cap)
     89 {
     90     LOGV("video encoder cap:");
     91     LOGV("codec = %d", cap.mCodec);
     92     LOGV("bit rate: min = %d and max = %d", cap.mMinBitRate, cap.mMaxBitRate);
     93     LOGV("frame width: min = %d and max = %d", cap.mMinFrameWidth, cap.mMaxFrameWidth);
     94     LOGV("frame height: min = %d and max = %d", cap.mMinFrameHeight, cap.mMaxFrameHeight);
     95     LOGV("frame rate: min = %d and max = %d", cap.mMinFrameRate, cap.mMaxFrameRate);
     96 }
     97 
     98 /*static*/ void
     99 MediaProfiles::logAudioEncoderCap(const MediaProfiles::AudioEncoderCap& cap)
    100 {
    101     LOGV("audio encoder cap:");
    102     LOGV("codec = %d", cap.mCodec);
    103     LOGV("bit rate: min = %d and max = %d", cap.mMinBitRate, cap.mMaxBitRate);
    104     LOGV("sample rate: min = %d and max = %d", cap.mMinSampleRate, cap.mMaxSampleRate);
    105     LOGV("number of channels: min = %d and max = %d", cap.mMinChannels, cap.mMaxChannels);
    106 }
    107 
    108 /*static*/ void
    109 MediaProfiles::logVideoDecoderCap(const MediaProfiles::VideoDecoderCap& cap)
    110 {
    111     LOGV("video decoder cap:");
    112     LOGV("codec = %d", cap.mCodec);
    113 }
    114 
    115 /*static*/ void
    116 MediaProfiles::logAudioDecoderCap(const MediaProfiles::AudioDecoderCap& cap)
    117 {
    118     LOGV("audio codec cap:");
    119     LOGV("codec = %d", cap.mCodec);
    120 }
    121 
    122 /*static*/ int
    123 MediaProfiles::findTagForName(const MediaProfiles::NameToTagMap *map, size_t nMappings, const char *name)
    124 {
    125     int tag = -1;
    126     for (size_t i = 0; i < nMappings; ++i) {
    127         if (!strcmp(map[i].name, name)) {
    128             tag = map[i].tag;
    129             break;
    130         }
    131     }
    132     return tag;
    133 }
    134 
    135 /*static*/ MediaProfiles::VideoCodec*
    136 MediaProfiles::createVideoCodec(const char **atts, MediaProfiles *profiles)
    137 {
    138     CHECK(!strcmp("codec",     atts[0]) &&
    139           !strcmp("bitRate",   atts[2]) &&
    140           !strcmp("width",     atts[4]) &&
    141           !strcmp("height",    atts[6]) &&
    142           !strcmp("frameRate", atts[8]));
    143 
    144     const size_t nMappings = sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
    145     const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
    146     CHECK(codec != -1);
    147 
    148     MediaProfiles::VideoCodec *videoCodec =
    149         new MediaProfiles::VideoCodec(static_cast<video_encoder>(codec),
    150             atoi(atts[3]), atoi(atts[5]), atoi(atts[7]), atoi(atts[9]));
    151     logVideoCodec(*videoCodec);
    152 
    153     size_t nCamcorderProfiles;
    154     CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
    155     profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mVideoCodec = videoCodec;
    156     return videoCodec;
    157 }
    158 
    159 /*static*/ MediaProfiles::AudioCodec*
    160 MediaProfiles::createAudioCodec(const char **atts, MediaProfiles *profiles)
    161 {
    162     CHECK(!strcmp("codec",      atts[0]) &&
    163           !strcmp("bitRate",    atts[2]) &&
    164           !strcmp("sampleRate", atts[4]) &&
    165           !strcmp("channels",   atts[6]));
    166     const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
    167     const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
    168     CHECK(codec != -1);
    169 
    170     MediaProfiles::AudioCodec *audioCodec =
    171         new MediaProfiles::AudioCodec(static_cast<audio_encoder>(codec),
    172             atoi(atts[3]), atoi(atts[5]), atoi(atts[7]));
    173     logAudioCodec(*audioCodec);
    174 
    175     size_t nCamcorderProfiles;
    176     CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
    177     profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mAudioCodec = audioCodec;
    178     return audioCodec;
    179 }
    180 /*static*/ MediaProfiles::AudioDecoderCap*
    181 MediaProfiles::createAudioDecoderCap(const char **atts)
    182 {
    183     CHECK(!strcmp("name",    atts[0]) &&
    184           !strcmp("enabled", atts[2]));
    185 
    186     const size_t nMappings = sizeof(sAudioDecoderNameMap)/sizeof(sAudioDecoderNameMap[0]);
    187     const int codec = findTagForName(sAudioDecoderNameMap, nMappings, atts[1]);
    188     CHECK(codec != -1);
    189 
    190     MediaProfiles::AudioDecoderCap *cap =
    191         new MediaProfiles::AudioDecoderCap(static_cast<audio_decoder>(codec));
    192     logAudioDecoderCap(*cap);
    193     return cap;
    194 }
    195 
    196 /*static*/ MediaProfiles::VideoDecoderCap*
    197 MediaProfiles::createVideoDecoderCap(const char **atts)
    198 {
    199     CHECK(!strcmp("name",    atts[0]) &&
    200           !strcmp("enabled", atts[2]));
    201 
    202     const size_t nMappings = sizeof(sVideoDecoderNameMap)/sizeof(sVideoDecoderNameMap[0]);
    203     const int codec = findTagForName(sVideoDecoderNameMap, nMappings, atts[1]);
    204     CHECK(codec != -1);
    205 
    206     MediaProfiles::VideoDecoderCap *cap =
    207         new MediaProfiles::VideoDecoderCap(static_cast<video_decoder>(codec));
    208     logVideoDecoderCap(*cap);
    209     return cap;
    210 }
    211 
    212 /*static*/ MediaProfiles::VideoEncoderCap*
    213 MediaProfiles::createVideoEncoderCap(const char **atts)
    214 {
    215     CHECK(!strcmp("name",           atts[0])  &&
    216           !strcmp("enabled",        atts[2])  &&
    217           !strcmp("minBitRate",     atts[4])  &&
    218           !strcmp("maxBitRate",     atts[6])  &&
    219           !strcmp("minFrameWidth",  atts[8])  &&
    220           !strcmp("maxFrameWidth",  atts[10]) &&
    221           !strcmp("minFrameHeight", atts[12]) &&
    222           !strcmp("maxFrameHeight", atts[14]) &&
    223           !strcmp("minFrameRate",   atts[16]) &&
    224           !strcmp("maxFrameRate",   atts[18]));
    225 
    226     const size_t nMappings = sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
    227     const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
    228     CHECK(codec != -1);
    229 
    230     MediaProfiles::VideoEncoderCap *cap =
    231         new MediaProfiles::VideoEncoderCap(static_cast<video_encoder>(codec),
    232             atoi(atts[5]), atoi(atts[7]), atoi(atts[9]), atoi(atts[11]), atoi(atts[13]),
    233             atoi(atts[15]), atoi(atts[17]), atoi(atts[19]));
    234     logVideoEncoderCap(*cap);
    235     return cap;
    236 }
    237 
    238 /*static*/ MediaProfiles::AudioEncoderCap*
    239 MediaProfiles::createAudioEncoderCap(const char **atts)
    240 {
    241     CHECK(!strcmp("name",          atts[0])  &&
    242           !strcmp("enabled",       atts[2])  &&
    243           !strcmp("minBitRate",    atts[4])  &&
    244           !strcmp("maxBitRate",    atts[6])  &&
    245           !strcmp("minSampleRate", atts[8])  &&
    246           !strcmp("maxSampleRate", atts[10]) &&
    247           !strcmp("minChannels",   atts[12]) &&
    248           !strcmp("maxChannels",   atts[14]));
    249 
    250     const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
    251     const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
    252     CHECK(codec != -1);
    253 
    254     MediaProfiles::AudioEncoderCap *cap =
    255         new MediaProfiles::AudioEncoderCap(static_cast<audio_encoder>(codec), atoi(atts[5]), atoi(atts[7]),
    256             atoi(atts[9]), atoi(atts[11]), atoi(atts[13]),
    257             atoi(atts[15]));
    258     logAudioEncoderCap(*cap);
    259     return cap;
    260 }
    261 
    262 /*static*/ output_format
    263 MediaProfiles::createEncoderOutputFileFormat(const char **atts)
    264 {
    265     CHECK(!strcmp("name", atts[0]));
    266 
    267     const size_t nMappings =sizeof(sFileFormatMap)/sizeof(sFileFormatMap[0]);
    268     const int format = findTagForName(sFileFormatMap, nMappings, atts[1]);
    269     CHECK(format != -1);
    270 
    271     return static_cast<output_format>(format);
    272 }
    273 
    274 /*static*/ MediaProfiles::CamcorderProfile*
    275 MediaProfiles::createCamcorderProfile(int cameraId, const char **atts)
    276 {
    277     CHECK(!strcmp("quality",    atts[0]) &&
    278           !strcmp("fileFormat", atts[2]) &&
    279           !strcmp("duration",   atts[4]));
    280 
    281     const size_t nProfileMappings = sizeof(sCamcorderQualityNameMap)/sizeof(sCamcorderQualityNameMap[0]);
    282     const int quality = findTagForName(sCamcorderQualityNameMap, nProfileMappings, atts[1]);
    283     CHECK(quality != -1);
    284 
    285     const size_t nFormatMappings = sizeof(sFileFormatMap)/sizeof(sFileFormatMap[0]);
    286     const int fileFormat = findTagForName(sFileFormatMap, nFormatMappings, atts[3]);
    287     CHECK(fileFormat != -1);
    288 
    289     MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
    290     profile->mCameraId = cameraId;
    291     profile->mFileFormat = static_cast<output_format>(fileFormat);
    292     profile->mQuality = static_cast<camcorder_quality>(quality);
    293     profile->mDuration = atoi(atts[5]);
    294     return profile;
    295 }
    296 
    297 MediaProfiles::ImageEncodingQualityLevels*
    298 MediaProfiles::findImageEncodingQualityLevels(int cameraId) const
    299 {
    300     int n = mImageEncodingQualityLevels.size();
    301     for (int i = 0; i < n; i++) {
    302         ImageEncodingQualityLevels *levels = mImageEncodingQualityLevels[i];
    303         if (levels->mCameraId == cameraId) {
    304             return levels;
    305         }
    306     }
    307     return NULL;
    308 }
    309 
    310 void MediaProfiles::addImageEncodingQualityLevel(int cameraId, const char** atts)
    311 {
    312     CHECK(!strcmp("quality", atts[0]));
    313     int quality = atoi(atts[1]);
    314     LOGV("%s: cameraId=%d, quality=%d\n", __func__, cameraId, quality);
    315     ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
    316 
    317     if (levels == NULL) {
    318         levels = new ImageEncodingQualityLevels();
    319         levels->mCameraId = cameraId;
    320         mImageEncodingQualityLevels.add(levels);
    321     }
    322 
    323     levels->mLevels.add(quality);
    324 }
    325 
    326 /*static*/ int
    327 MediaProfiles::getCameraId(const char** atts)
    328 {
    329     if (!atts[0]) return 0;  // default cameraId = 0
    330     CHECK(!strcmp("cameraId", atts[0]));
    331     return atoi(atts[1]);
    332 }
    333 
    334 /*static*/ void
    335 MediaProfiles::startElementHandler(void *userData, const char *name, const char **atts)
    336 {
    337     MediaProfiles *profiles = (MediaProfiles *) userData;
    338     if (strcmp("Video", name) == 0) {
    339         createVideoCodec(atts, profiles);
    340     } else if (strcmp("Audio", name) == 0) {
    341         createAudioCodec(atts, profiles);
    342     } else if (strcmp("VideoEncoderCap", name) == 0 &&
    343                strcmp("true", atts[3]) == 0) {
    344         profiles->mVideoEncoders.add(createVideoEncoderCap(atts));
    345     } else if (strcmp("AudioEncoderCap", name) == 0 &&
    346                strcmp("true", atts[3]) == 0) {
    347         profiles->mAudioEncoders.add(createAudioEncoderCap(atts));
    348     } else if (strcmp("VideoDecoderCap", name) == 0 &&
    349                strcmp("true", atts[3]) == 0) {
    350         profiles->mVideoDecoders.add(createVideoDecoderCap(atts));
    351     } else if (strcmp("AudioDecoderCap", name) == 0 &&
    352                strcmp("true", atts[3]) == 0) {
    353         profiles->mAudioDecoders.add(createAudioDecoderCap(atts));
    354     } else if (strcmp("EncoderOutputFileFormat", name) == 0) {
    355         profiles->mEncoderOutputFileFormats.add(createEncoderOutputFileFormat(atts));
    356     } else if (strcmp("CamcorderProfiles", name) == 0) {
    357         profiles->mCurrentCameraId = getCameraId(atts);
    358     } else if (strcmp("EncoderProfile", name) == 0) {
    359         profiles->mCamcorderProfiles.add(
    360             createCamcorderProfile(profiles->mCurrentCameraId, atts));
    361     } else if (strcmp("ImageEncoding", name) == 0) {
    362         profiles->addImageEncodingQualityLevel(profiles->mCurrentCameraId, atts);
    363     }
    364 }
    365 
    366 /*static*/ MediaProfiles*
    367 MediaProfiles::getInstance()
    368 {
    369     LOGV("getInstance");
    370     Mutex::Autolock lock(sLock);
    371     if (!sIsInitialized) {
    372         char value[PROPERTY_VALUE_MAX];
    373         if (property_get("media.settings.xml", value, NULL) <= 0) {
    374             const char *defaultXmlFile = "/etc/media_profiles.xml";
    375             FILE *fp = fopen(defaultXmlFile, "r");
    376             if (fp == NULL) {
    377                 LOGW("could not find media config xml file");
    378                 sInstance = createDefaultInstance();
    379             } else {
    380                 fclose(fp);  // close the file first.
    381                 sInstance = createInstanceFromXmlFile(defaultXmlFile);
    382             }
    383         } else {
    384             sInstance = createInstanceFromXmlFile(value);
    385         }
    386     }
    387 
    388     return sInstance;
    389 }
    390 
    391 /*static*/ MediaProfiles::VideoEncoderCap*
    392 MediaProfiles::createDefaultH263VideoEncoderCap()
    393 {
    394     return new MediaProfiles::VideoEncoderCap(
    395         VIDEO_ENCODER_H263, 192000, 420000, 176, 352, 144, 288, 1, 20);
    396 }
    397 
    398 /*static*/ MediaProfiles::VideoEncoderCap*
    399 MediaProfiles::createDefaultM4vVideoEncoderCap()
    400 {
    401     return new MediaProfiles::VideoEncoderCap(
    402         VIDEO_ENCODER_MPEG_4_SP, 192000, 420000, 176, 352, 144, 288, 1, 20);
    403 }
    404 
    405 
    406 /*static*/ void
    407 MediaProfiles::createDefaultVideoEncoders(MediaProfiles *profiles)
    408 {
    409     profiles->mVideoEncoders.add(createDefaultH263VideoEncoderCap());
    410     profiles->mVideoEncoders.add(createDefaultM4vVideoEncoderCap());
    411 }
    412 
    413 /*static*/ MediaProfiles::CamcorderProfile*
    414 MediaProfiles::createDefaultCamcorderHighProfile()
    415 {
    416     MediaProfiles::VideoCodec *videoCodec =
    417         new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 360000, 352, 288, 20);
    418 
    419     AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
    420     CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
    421     profile->mCameraId = 0;
    422     profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
    423     profile->mQuality = CAMCORDER_QUALITY_HIGH;
    424     profile->mDuration = 60;
    425     profile->mVideoCodec = videoCodec;
    426     profile->mAudioCodec = audioCodec;
    427     return profile;
    428 }
    429 
    430 /*static*/ MediaProfiles::CamcorderProfile*
    431 MediaProfiles::createDefaultCamcorderLowProfile()
    432 {
    433     MediaProfiles::VideoCodec *videoCodec =
    434         new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 192000, 176, 144, 20);
    435 
    436     MediaProfiles::AudioCodec *audioCodec =
    437         new MediaProfiles::AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
    438 
    439     MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
    440     profile->mCameraId = 0;
    441     profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
    442     profile->mQuality = CAMCORDER_QUALITY_LOW;
    443     profile->mDuration = 30;
    444     profile->mVideoCodec = videoCodec;
    445     profile->mAudioCodec = audioCodec;
    446     return profile;
    447 }
    448 
    449 /*static*/ void
    450 MediaProfiles::createDefaultCamcorderProfiles(MediaProfiles *profiles)
    451 {
    452     profiles->mCamcorderProfiles.add(createDefaultCamcorderHighProfile());
    453     profiles->mCamcorderProfiles.add(createDefaultCamcorderLowProfile());
    454 }
    455 
    456 /*static*/ void
    457 MediaProfiles::createDefaultAudioEncoders(MediaProfiles *profiles)
    458 {
    459     profiles->mAudioEncoders.add(createDefaultAmrNBEncoderCap());
    460 }
    461 
    462 /*static*/ void
    463 MediaProfiles::createDefaultVideoDecoders(MediaProfiles *profiles)
    464 {
    465     MediaProfiles::VideoDecoderCap *cap =
    466         new MediaProfiles::VideoDecoderCap(VIDEO_DECODER_WMV);
    467 
    468     profiles->mVideoDecoders.add(cap);
    469 }
    470 
    471 /*static*/ void
    472 MediaProfiles::createDefaultAudioDecoders(MediaProfiles *profiles)
    473 {
    474     MediaProfiles::AudioDecoderCap *cap =
    475         new MediaProfiles::AudioDecoderCap(AUDIO_DECODER_WMA);
    476 
    477     profiles->mAudioDecoders.add(cap);
    478 }
    479 
    480 /*static*/ void
    481 MediaProfiles::createDefaultEncoderOutputFileFormats(MediaProfiles *profiles)
    482 {
    483     profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_THREE_GPP);
    484     profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_MPEG_4);
    485 }
    486 
    487 /*static*/ MediaProfiles::AudioEncoderCap*
    488 MediaProfiles::createDefaultAmrNBEncoderCap()
    489 {
    490     return new MediaProfiles::AudioEncoderCap(
    491         AUDIO_ENCODER_AMR_NB, 5525, 12200, 8000, 8000, 1, 1);
    492 }
    493 
    494 /*static*/ void
    495 MediaProfiles::createDefaultImageEncodingQualityLevels(MediaProfiles *profiles)
    496 {
    497     ImageEncodingQualityLevels *levels = new ImageEncodingQualityLevels();
    498     levels->mCameraId = 0;
    499     levels->mLevels.add(70);
    500     levels->mLevels.add(80);
    501     levels->mLevels.add(90);
    502     profiles->mImageEncodingQualityLevels.add(levels);
    503 }
    504 
    505 /*static*/ MediaProfiles*
    506 MediaProfiles::createDefaultInstance()
    507 {
    508     MediaProfiles *profiles = new MediaProfiles;
    509     createDefaultCamcorderProfiles(profiles);
    510     createDefaultVideoEncoders(profiles);
    511     createDefaultAudioEncoders(profiles);
    512     createDefaultVideoDecoders(profiles);
    513     createDefaultAudioDecoders(profiles);
    514     createDefaultEncoderOutputFileFormats(profiles);
    515     createDefaultImageEncodingQualityLevels(profiles);
    516     sIsInitialized = true;
    517     return profiles;
    518 }
    519 
    520 /*static*/ MediaProfiles*
    521 MediaProfiles::createInstanceFromXmlFile(const char *xml)
    522 {
    523     FILE *fp = NULL;
    524     CHECK((fp = fopen(xml, "r")));
    525 
    526     XML_Parser parser = ::XML_ParserCreate(NULL);
    527     CHECK(parser != NULL);
    528 
    529     MediaProfiles *profiles = new MediaProfiles();
    530     ::XML_SetUserData(parser, profiles);
    531     ::XML_SetElementHandler(parser, startElementHandler, NULL);
    532 
    533     /*
    534       FIXME:
    535       expat is not compiled with -DXML_DTD. We don't have DTD parsing support.
    536 
    537       if (!::XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS)) {
    538           LOGE("failed to enable DTD support in the xml file");
    539           return UNKNOWN_ERROR;
    540       }
    541 
    542     */
    543 
    544     const int BUFF_SIZE = 512;
    545     for (;;) {
    546         void *buff = ::XML_GetBuffer(parser, BUFF_SIZE);
    547         if (buff == NULL) {
    548             LOGE("failed to in call to XML_GetBuffer()");
    549             delete profiles;
    550             profiles = NULL;
    551             goto exit;
    552         }
    553 
    554         int bytes_read = ::fread(buff, 1, BUFF_SIZE, fp);
    555         if (bytes_read < 0) {
    556             LOGE("failed in call to read");
    557             delete profiles;
    558             profiles = NULL;
    559             goto exit;
    560         }
    561 
    562         CHECK(::XML_ParseBuffer(parser, bytes_read, bytes_read == 0));
    563 
    564         if (bytes_read == 0) break;  // done parsing the xml file
    565     }
    566 
    567 exit:
    568     ::XML_ParserFree(parser);
    569     ::fclose(fp);
    570     if (profiles) {
    571         sIsInitialized = true;
    572     }
    573     return profiles;
    574 }
    575 
    576 Vector<output_format> MediaProfiles::getOutputFileFormats() const
    577 {
    578     return mEncoderOutputFileFormats;  // copy out
    579 }
    580 
    581 Vector<video_encoder> MediaProfiles::getVideoEncoders() const
    582 {
    583     Vector<video_encoder> encoders;
    584     for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
    585         encoders.add(mVideoEncoders[i]->mCodec);
    586     }
    587     return encoders;  // copy out
    588 }
    589 
    590 int MediaProfiles::getVideoEncoderParamByName(const char *name, video_encoder codec) const
    591 {
    592     LOGV("getVideoEncoderParamByName: %s for codec %d", name, codec);
    593     int index = -1;
    594     for (size_t i = 0, n = mVideoEncoders.size(); i < n; ++i) {
    595         if (mVideoEncoders[i]->mCodec == codec) {
    596             index = i;
    597             break;
    598         }
    599     }
    600     if (index == -1) {
    601         LOGE("The given video encoder %d is not found", codec);
    602         return -1;
    603     }
    604 
    605     if (!strcmp("enc.vid.width.min", name)) return mVideoEncoders[index]->mMinFrameWidth;
    606     if (!strcmp("enc.vid.width.max", name)) return mVideoEncoders[index]->mMaxFrameWidth;
    607     if (!strcmp("enc.vid.height.min", name)) return mVideoEncoders[index]->mMinFrameHeight;
    608     if (!strcmp("enc.vid.height.max", name)) return mVideoEncoders[index]->mMaxFrameHeight;
    609     if (!strcmp("enc.vid.bps.min", name)) return mVideoEncoders[index]->mMinBitRate;
    610     if (!strcmp("enc.vid.bps.max", name)) return mVideoEncoders[index]->mMaxBitRate;
    611     if (!strcmp("enc.vid.fps.min", name)) return mVideoEncoders[index]->mMinFrameRate;
    612     if (!strcmp("enc.vid.fps.max", name)) return mVideoEncoders[index]->mMaxFrameRate;
    613 
    614     LOGE("The given video encoder param name %s is not found", name);
    615     return -1;
    616 }
    617 
    618 Vector<audio_encoder> MediaProfiles::getAudioEncoders() const
    619 {
    620     Vector<audio_encoder> encoders;
    621     for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
    622         encoders.add(mAudioEncoders[i]->mCodec);
    623     }
    624     return encoders;  // copy out
    625 }
    626 
    627 int MediaProfiles::getAudioEncoderParamByName(const char *name, audio_encoder codec) const
    628 {
    629     LOGV("getAudioEncoderParamByName: %s for codec %d", name, codec);
    630     int index = -1;
    631     for (size_t i = 0, n = mAudioEncoders.size(); i < n; ++i) {
    632         if (mAudioEncoders[i]->mCodec == codec) {
    633             index = i;
    634             break;
    635         }
    636     }
    637     if (index == -1) {
    638         LOGE("The given audio encoder %d is not found", codec);
    639         return -1;
    640     }
    641 
    642     if (!strcmp("enc.aud.ch.min", name)) return mAudioEncoders[index]->mMinChannels;
    643     if (!strcmp("enc.aud.ch.max", name)) return mAudioEncoders[index]->mMaxChannels;
    644     if (!strcmp("enc.aud.bps.min", name)) return mAudioEncoders[index]->mMinBitRate;
    645     if (!strcmp("enc.aud.bps.max", name)) return mAudioEncoders[index]->mMaxBitRate;
    646     if (!strcmp("enc.aud.hz.min", name)) return mAudioEncoders[index]->mMinSampleRate;
    647     if (!strcmp("enc.aud.hz.max", name)) return mAudioEncoders[index]->mMaxSampleRate;
    648 
    649     LOGE("The given audio encoder param name %s is not found", name);
    650     return -1;
    651 }
    652 
    653 Vector<video_decoder> MediaProfiles::getVideoDecoders() const
    654 {
    655     Vector<video_decoder> decoders;
    656     for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
    657         decoders.add(mVideoDecoders[i]->mCodec);
    658     }
    659     return decoders;  // copy out
    660 }
    661 
    662 Vector<audio_decoder> MediaProfiles::getAudioDecoders() const
    663 {
    664     Vector<audio_decoder> decoders;
    665     for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
    666         decoders.add(mAudioDecoders[i]->mCodec);
    667     }
    668     return decoders;  // copy out
    669 }
    670 
    671 int MediaProfiles::getCamcorderProfileParamByName(const char *name,
    672                                                   int cameraId,
    673                                                   camcorder_quality quality) const
    674 {
    675     LOGV("getCamcorderProfileParamByName: %s for camera %d, quality %d",
    676          name, cameraId, quality);
    677 
    678     int index = -1;
    679     for (size_t i = 0, n = mCamcorderProfiles.size(); i < n; ++i) {
    680         if (mCamcorderProfiles[i]->mCameraId == cameraId &&
    681             mCamcorderProfiles[i]->mQuality == quality) {
    682             index = i;
    683             break;
    684         }
    685     }
    686     if (index == -1) {
    687         LOGE("The given camcorder profile camera %d quality %d is not found",
    688              cameraId, quality);
    689         return -1;
    690     }
    691 
    692     if (!strcmp("duration", name)) return mCamcorderProfiles[index]->mDuration;
    693     if (!strcmp("file.format", name)) return mCamcorderProfiles[index]->mFileFormat;
    694     if (!strcmp("vid.codec", name)) return mCamcorderProfiles[index]->mVideoCodec->mCodec;
    695     if (!strcmp("vid.width", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameWidth;
    696     if (!strcmp("vid.height", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameHeight;
    697     if (!strcmp("vid.bps", name)) return mCamcorderProfiles[index]->mVideoCodec->mBitRate;
    698     if (!strcmp("vid.fps", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameRate;
    699     if (!strcmp("aud.codec", name)) return mCamcorderProfiles[index]->mAudioCodec->mCodec;
    700     if (!strcmp("aud.bps", name)) return mCamcorderProfiles[index]->mAudioCodec->mBitRate;
    701     if (!strcmp("aud.ch", name)) return mCamcorderProfiles[index]->mAudioCodec->mChannels;
    702     if (!strcmp("aud.hz", name)) return mCamcorderProfiles[index]->mAudioCodec->mSampleRate;
    703 
    704     LOGE("The given camcorder profile param id %d name %s is not found", cameraId, name);
    705     return -1;
    706 }
    707 
    708 Vector<int> MediaProfiles::getImageEncodingQualityLevels(int cameraId) const
    709 {
    710     Vector<int> result;
    711     ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
    712     if (levels != NULL) {
    713         result = levels->mLevels;  // copy out
    714     }
    715     return result;
    716 }
    717 
    718 MediaProfiles::~MediaProfiles()
    719 {
    720     CHECK("destructor should never be called" == 0);
    721 #if 0
    722     for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
    723         delete mAudioEncoders[i];
    724     }
    725     mAudioEncoders.clear();
    726 
    727     for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
    728         delete mVideoEncoders[i];
    729     }
    730     mVideoEncoders.clear();
    731 
    732     for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
    733         delete mVideoDecoders[i];
    734     }
    735     mVideoDecoders.clear();
    736 
    737     for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
    738         delete mAudioDecoders[i];
    739     }
    740     mAudioDecoders.clear();
    741 
    742     for (size_t i = 0; i < mCamcorderProfiles.size(); ++i) {
    743         delete mCamcorderProfiles[i];
    744     }
    745     mCamcorderProfiles.clear();
    746 #endif
    747 }
    748 } // namespace android
    749