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/foundation/ADebug.h>
29 #include <OMX_Video.h>
30 #include <sys/stat.h>
31 
32 #include <array>
33 #include <string>
34 #include <vector>
35 
36 namespace android {
37 
38 namespace /* unnamed */ {
39 
40 // Returns a list of possible paths for the media_profiles XML file.
getXmlPaths()41 std::array<char const*, 5> const& getXmlPaths() {
42     static std::array<std::string const, 5> const paths =
43         []() -> decltype(paths) {
44             // Directories for XML file that will be searched (in this order).
45             constexpr std::array<char const*, 4> searchDirs = {
46                 "product/etc/",
47                 "odm/etc/",
48                 "vendor/etc/",
49                 "system/etc/",
50             };
51 
52             // The file name may contain a variant if the vendor property
53             // ro.vendor.media_profiles_xml_variant is set.
54             char variant[PROPERTY_VALUE_MAX];
55             property_get("ro.media.xml_variant.profiles",
56                          variant,
57                          "_V1_0");
58 
59             std::string fileName =
60                 std::string("media_profiles") + variant + ".xml";
61 
62             return { searchDirs[0] + fileName,
63                      searchDirs[1] + fileName,
64                      searchDirs[2] + fileName,
65                      searchDirs[3] + fileName,
66                      "system/etc/media_profiles_V1_0.xml" // System fallback
67                    };
68         }();
69     static std::array<char const*, 5> const cPaths = {
70             paths[0].data(),
71             paths[1].data(),
72             paths[2].data(),
73             paths[3].data(),
74             paths[4].data()
75         };
76     return cPaths;
77 }
78 
79 } // unnamed namespace
80 
81 Mutex MediaProfiles::sLock;
82 bool MediaProfiles::sIsInitialized = false;
83 MediaProfiles *MediaProfiles::sInstance = NULL;
84 
85 const MediaProfiles::NameToTagMap MediaProfiles::sVideoEncoderNameMap[] = {
86     {"h263", VIDEO_ENCODER_H263},
87     {"h264", VIDEO_ENCODER_H264},
88     {"m4v",  VIDEO_ENCODER_MPEG_4_SP},
89     {"hevc", VIDEO_ENCODER_HEVC}
90 };
91 
92 const MediaProfiles::NameToTagMap MediaProfiles::sAudioEncoderNameMap[] = {
93     {"amrnb",  AUDIO_ENCODER_AMR_NB},
94     {"amrwb",  AUDIO_ENCODER_AMR_WB},
95     {"aac",    AUDIO_ENCODER_AAC},
96     {"heaac",  AUDIO_ENCODER_HE_AAC},
97     {"aaceld", AUDIO_ENCODER_AAC_ELD},
98     {"opus",   AUDIO_ENCODER_OPUS}
99 };
100 
101 const MediaProfiles::NameToTagMap MediaProfiles::sFileFormatMap[] = {
102     {"3gp", OUTPUT_FORMAT_THREE_GPP},
103     {"mp4", OUTPUT_FORMAT_MPEG_4}
104 };
105 
106 const MediaProfiles::NameToTagMap MediaProfiles::sVideoDecoderNameMap[] = {
107     {"wmv", VIDEO_DECODER_WMV}
108 };
109 
110 const MediaProfiles::NameToTagMap MediaProfiles::sAudioDecoderNameMap[] = {
111     {"wma", AUDIO_DECODER_WMA}
112 };
113 
114 const MediaProfiles::NameToTagMap MediaProfiles::sCamcorderQualityNameMap[] = {
115     {"low", CAMCORDER_QUALITY_LOW},
116     {"high", CAMCORDER_QUALITY_HIGH},
117     {"qcif", CAMCORDER_QUALITY_QCIF},
118     {"cif", CAMCORDER_QUALITY_CIF},
119     {"480p", CAMCORDER_QUALITY_480P},
120     {"720p", CAMCORDER_QUALITY_720P},
121     {"1080p", CAMCORDER_QUALITY_1080P},
122     {"2160p", CAMCORDER_QUALITY_2160P},
123     {"qvga", CAMCORDER_QUALITY_QVGA},
124 
125     {"timelapselow",  CAMCORDER_QUALITY_TIME_LAPSE_LOW},
126     {"timelapsehigh", CAMCORDER_QUALITY_TIME_LAPSE_HIGH},
127     {"timelapseqcif", CAMCORDER_QUALITY_TIME_LAPSE_QCIF},
128     {"timelapsecif", CAMCORDER_QUALITY_TIME_LAPSE_CIF},
129     {"timelapse480p", CAMCORDER_QUALITY_TIME_LAPSE_480P},
130     {"timelapse720p", CAMCORDER_QUALITY_TIME_LAPSE_720P},
131     {"timelapse1080p", CAMCORDER_QUALITY_TIME_LAPSE_1080P},
132     {"timelapse2160p", CAMCORDER_QUALITY_TIME_LAPSE_2160P},
133     {"timelapseqvga", CAMCORDER_QUALITY_TIME_LAPSE_QVGA},
134 
135     {"highspeedlow",  CAMCORDER_QUALITY_HIGH_SPEED_LOW},
136     {"highspeedhigh", CAMCORDER_QUALITY_HIGH_SPEED_HIGH},
137     {"highspeed480p", CAMCORDER_QUALITY_HIGH_SPEED_480P},
138     {"highspeed720p", CAMCORDER_QUALITY_HIGH_SPEED_720P},
139     {"highspeed1080p", CAMCORDER_QUALITY_HIGH_SPEED_1080P},
140     {"highspeed2160p", CAMCORDER_QUALITY_HIGH_SPEED_2160P},
141 
142     // Vendor-specific profiles
143     {"vga", CAMCORDER_QUALITY_VGA},
144     {"4kdci", CAMCORDER_QUALITY_4KDCI},
145     {"timelapsevga", CAMCORDER_QUALITY_TIME_LAPSE_VGA},
146     {"timelapse4kdci", CAMCORDER_QUALITY_TIME_LAPSE_4KDCI},
147     {"highspeedcif", CAMCORDER_QUALITY_HIGH_SPEED_CIF},
148     {"highspeedvga", CAMCORDER_QUALITY_HIGH_SPEED_VGA},
149     {"highspeed4kdci", CAMCORDER_QUALITY_HIGH_SPEED_4KDCI},
150     {"qhd", CAMCORDER_QUALITY_QHD},
151     {"2k", CAMCORDER_QUALITY_2k},
152     {"timelapseqhd", CAMCORDER_QUALITY_TIME_LAPSE_QHD},
153     {"timelapse2k", CAMCORDER_QUALITY_TIME_LAPSE_2k},
154 };
155 
156 #if LOG_NDEBUG
157 #define UNUSED __unused
158 #else
159 #define UNUSED
160 #endif
161 
162 /*static*/ void
logVideoCodec(const MediaProfiles::VideoCodec & codec UNUSED)163 MediaProfiles::logVideoCodec(const MediaProfiles::VideoCodec& codec UNUSED)
164 {
165     ALOGV("video codec:");
166     ALOGV("codec = %d", codec.mCodec);
167     ALOGV("bit rate: %d", codec.mBitRate);
168     ALOGV("frame width: %d", codec.mFrameWidth);
169     ALOGV("frame height: %d", codec.mFrameHeight);
170     ALOGV("frame rate: %d", codec.mFrameRate);
171 }
172 
173 /*static*/ void
logAudioCodec(const MediaProfiles::AudioCodec & codec UNUSED)174 MediaProfiles::logAudioCodec(const MediaProfiles::AudioCodec& codec UNUSED)
175 {
176     ALOGV("audio codec:");
177     ALOGV("codec = %d", codec.mCodec);
178     ALOGV("bit rate: %d", codec.mBitRate);
179     ALOGV("sample rate: %d", codec.mSampleRate);
180     ALOGV("number of channels: %d", codec.mChannels);
181 }
182 
183 /*static*/ void
logVideoEncoderCap(const MediaProfiles::VideoEncoderCap & cap UNUSED)184 MediaProfiles::logVideoEncoderCap(const MediaProfiles::VideoEncoderCap& cap UNUSED)
185 {
186     ALOGV("video encoder cap:");
187     ALOGV("codec = %d", cap.mCodec);
188     ALOGV("bit rate: min = %d and max = %d", cap.mMinBitRate, cap.mMaxBitRate);
189     ALOGV("frame width: min = %d and max = %d", cap.mMinFrameWidth, cap.mMaxFrameWidth);
190     ALOGV("frame height: min = %d and max = %d", cap.mMinFrameHeight, cap.mMaxFrameHeight);
191     ALOGV("frame rate: min = %d and max = %d", cap.mMinFrameRate, cap.mMaxFrameRate);
192 }
193 
194 /*static*/ void
logAudioEncoderCap(const MediaProfiles::AudioEncoderCap & cap UNUSED)195 MediaProfiles::logAudioEncoderCap(const MediaProfiles::AudioEncoderCap& cap UNUSED)
196 {
197     ALOGV("audio encoder cap:");
198     ALOGV("codec = %d", cap.mCodec);
199     ALOGV("bit rate: min = %d and max = %d", cap.mMinBitRate, cap.mMaxBitRate);
200     ALOGV("sample rate: min = %d and max = %d", cap.mMinSampleRate, cap.mMaxSampleRate);
201     ALOGV("number of channels: min = %d and max = %d", cap.mMinChannels, cap.mMaxChannels);
202 }
203 
204 /*static*/ void
logVideoDecoderCap(const MediaProfiles::VideoDecoderCap & cap UNUSED)205 MediaProfiles::logVideoDecoderCap(const MediaProfiles::VideoDecoderCap& cap UNUSED)
206 {
207     ALOGV("video decoder cap:");
208     ALOGV("codec = %d", cap.mCodec);
209 }
210 
211 /*static*/ void
logAudioDecoderCap(const MediaProfiles::AudioDecoderCap & cap UNUSED)212 MediaProfiles::logAudioDecoderCap(const MediaProfiles::AudioDecoderCap& cap UNUSED)
213 {
214     ALOGV("audio codec cap:");
215     ALOGV("codec = %d", cap.mCodec);
216 }
217 
218 /*static*/ int
findTagForName(const MediaProfiles::NameToTagMap * map,size_t nMappings,const char * name)219 MediaProfiles::findTagForName(const MediaProfiles::NameToTagMap *map, size_t nMappings,
220         const char *name)
221 {
222     int tag = -1;
223     for (size_t i = 0; i < nMappings; ++i) {
224         if (!strcmp(map[i].name, name)) {
225             tag = map[i].tag;
226             break;
227         }
228     }
229     return tag;
230 }
231 
232 /*static*/ MediaProfiles::VideoCodec*
createVideoCodec(const char ** atts,MediaProfiles * profiles)233 MediaProfiles::createVideoCodec(const char **atts, MediaProfiles *profiles)
234 {
235     CHECK(!strcmp("codec",     atts[0]) &&
236           !strcmp("bitRate",   atts[2]) &&
237           !strcmp("width",     atts[4]) &&
238           !strcmp("height",    atts[6]) &&
239           !strcmp("frameRate", atts[8]));
240 
241     const size_t nMappings = sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
242     const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
243     CHECK(codec != -1);
244 
245     MediaProfiles::VideoCodec *videoCodec =
246         new MediaProfiles::VideoCodec(static_cast<video_encoder>(codec),
247             atoi(atts[3]), atoi(atts[5]), atoi(atts[7]), atoi(atts[9]));
248     logVideoCodec(*videoCodec);
249 
250     size_t nCamcorderProfiles;
251     CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
252     profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mVideoCodec = videoCodec;
253     return videoCodec;
254 }
255 
256 /*static*/ MediaProfiles::AudioCodec*
createAudioCodec(const char ** atts,MediaProfiles * profiles)257 MediaProfiles::createAudioCodec(const char **atts, MediaProfiles *profiles)
258 {
259     CHECK(!strcmp("codec",      atts[0]) &&
260           !strcmp("bitRate",    atts[2]) &&
261           !strcmp("sampleRate", atts[4]) &&
262           !strcmp("channels",   atts[6]));
263     const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
264     const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
265     CHECK(codec != -1);
266 
267     MediaProfiles::AudioCodec *audioCodec =
268         new MediaProfiles::AudioCodec(static_cast<audio_encoder>(codec),
269             atoi(atts[3]), atoi(atts[5]), atoi(atts[7]));
270     logAudioCodec(*audioCodec);
271 
272     size_t nCamcorderProfiles;
273     CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
274     profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mAudioCodec = audioCodec;
275     return audioCodec;
276 }
277 /*static*/ MediaProfiles::AudioDecoderCap*
createAudioDecoderCap(const char ** atts)278 MediaProfiles::createAudioDecoderCap(const char **atts)
279 {
280     CHECK(!strcmp("name",    atts[0]) &&
281           !strcmp("enabled", atts[2]));
282 
283     const size_t nMappings = sizeof(sAudioDecoderNameMap)/sizeof(sAudioDecoderNameMap[0]);
284     const int codec = findTagForName(sAudioDecoderNameMap, nMappings, atts[1]);
285     CHECK(codec != -1);
286 
287     MediaProfiles::AudioDecoderCap *cap =
288         new MediaProfiles::AudioDecoderCap(static_cast<audio_decoder>(codec));
289     logAudioDecoderCap(*cap);
290     return cap;
291 }
292 
293 /*static*/ MediaProfiles::VideoDecoderCap*
createVideoDecoderCap(const char ** atts)294 MediaProfiles::createVideoDecoderCap(const char **atts)
295 {
296     CHECK(!strcmp("name",    atts[0]) &&
297           !strcmp("enabled", atts[2]));
298 
299     const size_t nMappings = sizeof(sVideoDecoderNameMap)/sizeof(sVideoDecoderNameMap[0]);
300     const int codec = findTagForName(sVideoDecoderNameMap, nMappings, atts[1]);
301     CHECK(codec != -1);
302 
303     MediaProfiles::VideoDecoderCap *cap =
304         new MediaProfiles::VideoDecoderCap(static_cast<video_decoder>(codec));
305     logVideoDecoderCap(*cap);
306     return cap;
307 }
308 
309 /*static*/ MediaProfiles::VideoEncoderCap*
createVideoEncoderCap(const char ** atts)310 MediaProfiles::createVideoEncoderCap(const char **atts)
311 {
312     CHECK(!strcmp("name",           atts[0])  &&
313           !strcmp("enabled",        atts[2])  &&
314           !strcmp("minBitRate",     atts[4])  &&
315           !strcmp("maxBitRate",     atts[6])  &&
316           !strcmp("minFrameWidth",  atts[8])  &&
317           !strcmp("maxFrameWidth",  atts[10]) &&
318           !strcmp("minFrameHeight", atts[12]) &&
319           !strcmp("maxFrameHeight", atts[14]) &&
320           !strcmp("minFrameRate",   atts[16]) &&
321           !strcmp("maxFrameRate",   atts[18]));
322 
323     const size_t nMappings = sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
324     const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
325     CHECK(codec != -1);
326 
327     MediaProfiles::VideoEncoderCap *cap =
328         new MediaProfiles::VideoEncoderCap(static_cast<video_encoder>(codec),
329             atoi(atts[5]), atoi(atts[7]), atoi(atts[9]), atoi(atts[11]), atoi(atts[13]),
330             atoi(atts[15]), atoi(atts[17]), atoi(atts[19]));
331     logVideoEncoderCap(*cap);
332     return cap;
333 }
334 
335 /*static*/ MediaProfiles::AudioEncoderCap*
createAudioEncoderCap(const char ** atts)336 MediaProfiles::createAudioEncoderCap(const char **atts)
337 {
338     CHECK(!strcmp("name",          atts[0])  &&
339           !strcmp("enabled",       atts[2])  &&
340           !strcmp("minBitRate",    atts[4])  &&
341           !strcmp("maxBitRate",    atts[6])  &&
342           !strcmp("minSampleRate", atts[8])  &&
343           !strcmp("maxSampleRate", atts[10]) &&
344           !strcmp("minChannels",   atts[12]) &&
345           !strcmp("maxChannels",   atts[14]));
346 
347     const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
348     const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
349     CHECK(codec != -1);
350 
351     MediaProfiles::AudioEncoderCap *cap =
352         new MediaProfiles::AudioEncoderCap(static_cast<audio_encoder>(codec), atoi(atts[5]),
353             atoi(atts[7]), atoi(atts[9]), atoi(atts[11]), atoi(atts[13]), atoi(atts[15]));
354     logAudioEncoderCap(*cap);
355     return cap;
356 }
357 
358 /*static*/ output_format
createEncoderOutputFileFormat(const char ** atts)359 MediaProfiles::createEncoderOutputFileFormat(const char **atts)
360 {
361     CHECK(!strcmp("name", atts[0]));
362 
363     const size_t nMappings =sizeof(sFileFormatMap)/sizeof(sFileFormatMap[0]);
364     const int format = findTagForName(sFileFormatMap, nMappings, atts[1]);
365     CHECK(format != -1);
366 
367     return static_cast<output_format>(format);
368 }
369 
isCameraIdFound(int cameraId,const Vector<int> & cameraIds)370 static bool isCameraIdFound(int cameraId, const Vector<int>& cameraIds) {
371     for (int i = 0, n = cameraIds.size(); i < n; ++i) {
372         if (cameraId == cameraIds[i]) {
373             return true;
374         }
375     }
376     return false;
377 }
378 
379 /*static*/ MediaProfiles::CamcorderProfile*
createCamcorderProfile(int cameraId,const char ** atts,Vector<int> & cameraIds)380 MediaProfiles::createCamcorderProfile(int cameraId, const char **atts, Vector<int>& cameraIds)
381 {
382     CHECK(!strcmp("quality",    atts[0]) &&
383           !strcmp("fileFormat", atts[2]) &&
384           !strcmp("duration",   atts[4]));
385 
386     const size_t nProfileMappings = sizeof(sCamcorderQualityNameMap)/
387             sizeof(sCamcorderQualityNameMap[0]);
388     const int quality = findTagForName(sCamcorderQualityNameMap, nProfileMappings, atts[1]);
389     CHECK(quality != -1);
390 
391     const size_t nFormatMappings = sizeof(sFileFormatMap)/sizeof(sFileFormatMap[0]);
392     const int fileFormat = findTagForName(sFileFormatMap, nFormatMappings, atts[3]);
393     CHECK(fileFormat != -1);
394 
395     MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
396     profile->mCameraId = cameraId;
397     if (!isCameraIdFound(cameraId, cameraIds)) {
398         cameraIds.add(cameraId);
399     }
400     profile->mFileFormat = static_cast<output_format>(fileFormat);
401     profile->mQuality = static_cast<camcorder_quality>(quality);
402     profile->mDuration = atoi(atts[5]);
403     return profile;
404 }
405 
406 MediaProfiles::ImageEncodingQualityLevels*
findImageEncodingQualityLevels(int cameraId) const407 MediaProfiles::findImageEncodingQualityLevels(int cameraId) const
408 {
409     int n = mImageEncodingQualityLevels.size();
410     for (int i = 0; i < n; i++) {
411         ImageEncodingQualityLevels *levels = mImageEncodingQualityLevels[i];
412         if (levels->mCameraId == cameraId) {
413             return levels;
414         }
415     }
416     return NULL;
417 }
418 
addImageEncodingQualityLevel(int cameraId,const char ** atts)419 void MediaProfiles::addImageEncodingQualityLevel(int cameraId, const char** atts)
420 {
421     CHECK(!strcmp("quality", atts[0]));
422     int quality = atoi(atts[1]);
423     ALOGV("%s: cameraId=%d, quality=%d", __func__, cameraId, quality);
424     ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
425 
426     if (levels == NULL) {
427         levels = new ImageEncodingQualityLevels();
428         levels->mCameraId = cameraId;
429         mImageEncodingQualityLevels.add(levels);
430     }
431 
432     levels->mLevels.add(quality);
433 }
434 
435 /*static*/ int
getCameraId(const char ** atts)436 MediaProfiles::getCameraId(const char** atts)
437 {
438     if (!atts[0]) return 0;  // default cameraId = 0
439     CHECK(!strcmp("cameraId", atts[0]));
440     return atoi(atts[1]);
441 }
442 
addStartTimeOffset(int cameraId,const char ** atts)443 void MediaProfiles::addStartTimeOffset(int cameraId, const char** atts)
444 {
445     int offsetTimeMs = 1000;
446     if (atts[2]) {
447         CHECK(!strcmp("startOffsetMs", atts[2]));
448         offsetTimeMs = atoi(atts[3]);
449     }
450 
451     ALOGV("%s: cameraId=%d, offset=%d ms", __func__, cameraId, offsetTimeMs);
452     mStartTimeOffsets.replaceValueFor(cameraId, offsetTimeMs);
453 }
454 
455 /*static*/ void
startElementHandler(void * userData,const char * name,const char ** atts)456 MediaProfiles::startElementHandler(void *userData, const char *name, const char **atts)
457 {
458     MediaProfiles *profiles = (MediaProfiles *) userData;
459     if (strcmp("Video", name) == 0) {
460         createVideoCodec(atts, profiles);
461     } else if (strcmp("Audio", name) == 0) {
462         createAudioCodec(atts, profiles);
463     } else if (strcmp("VideoEncoderCap", name) == 0 &&
464                strcmp("true", atts[3]) == 0) {
465         profiles->mVideoEncoders.add(createVideoEncoderCap(atts));
466     } else if (strcmp("AudioEncoderCap", name) == 0 &&
467                strcmp("true", atts[3]) == 0) {
468         profiles->mAudioEncoders.add(createAudioEncoderCap(atts));
469     } else if (strcmp("VideoDecoderCap", name) == 0 &&
470                strcmp("true", atts[3]) == 0) {
471         profiles->mVideoDecoders.add(createVideoDecoderCap(atts));
472     } else if (strcmp("AudioDecoderCap", name) == 0 &&
473                strcmp("true", atts[3]) == 0) {
474         profiles->mAudioDecoders.add(createAudioDecoderCap(atts));
475     } else if (strcmp("EncoderOutputFileFormat", name) == 0) {
476         profiles->mEncoderOutputFileFormats.add(createEncoderOutputFileFormat(atts));
477     } else if (strcmp("CamcorderProfiles", name) == 0) {
478         profiles->mCurrentCameraId = getCameraId(atts);
479         profiles->addStartTimeOffset(profiles->mCurrentCameraId, atts);
480     } else if (strcmp("EncoderProfile", name) == 0) {
481         profiles->mCamcorderProfiles.add(
482             createCamcorderProfile(profiles->mCurrentCameraId, atts, profiles->mCameraIds));
483     } else if (strcmp("ImageEncoding", name) == 0) {
484         profiles->addImageEncodingQualityLevel(profiles->mCurrentCameraId, atts);
485     }
486 }
487 
isCamcorderProfile(camcorder_quality quality)488 static bool isCamcorderProfile(camcorder_quality quality) {
489     return quality >= CAMCORDER_QUALITY_LIST_START &&
490            quality <= CAMCORDER_QUALITY_LIST_END;
491 }
492 
isTimelapseProfile(camcorder_quality quality)493 static bool isTimelapseProfile(camcorder_quality quality) {
494     return quality >= CAMCORDER_QUALITY_TIME_LAPSE_LIST_START &&
495            quality <= CAMCORDER_QUALITY_TIME_LAPSE_LIST_END;
496 }
497 
isHighSpeedProfile(camcorder_quality quality)498 static bool isHighSpeedProfile(camcorder_quality quality) {
499     return quality >= CAMCORDER_QUALITY_HIGH_SPEED_LIST_START &&
500            quality <= CAMCORDER_QUALITY_HIGH_SPEED_LIST_END;
501 }
502 
initRequiredProfileRefs(const Vector<int> & cameraIds)503 void MediaProfiles::initRequiredProfileRefs(const Vector<int>& cameraIds) {
504     ALOGV("Number of camera ids: %zu", cameraIds.size());
505     CHECK(cameraIds.size() > 0);
506     mRequiredProfileRefs = new RequiredProfiles[cameraIds.size()];
507     for (size_t i = 0, n = cameraIds.size(); i < n; ++i) {
508         mRequiredProfileRefs[i].mCameraId = cameraIds[i];
509         for (size_t j = 0; j < kNumRequiredProfiles; ++j) {
510             mRequiredProfileRefs[i].mRefs[j].mHasRefProfile = false;
511             mRequiredProfileRefs[i].mRefs[j].mRefProfileIndex = -1;
512             if ((j & 1) == 0) {  // low resolution
513                 mRequiredProfileRefs[i].mRefs[j].mResolutionProduct = 0x7FFFFFFF;
514             } else {             // high resolution
515                 mRequiredProfileRefs[i].mRefs[j].mResolutionProduct = 0;
516             }
517         }
518     }
519 }
520 
getRequiredProfileRefIndex(int cameraId)521 int MediaProfiles::getRequiredProfileRefIndex(int cameraId) {
522     for (size_t i = 0, n = mCameraIds.size(); i < n; ++i) {
523         if (mCameraIds[i] == cameraId) {
524             return i;
525         }
526     }
527     return -1;
528 }
529 
checkAndAddRequiredProfilesIfNecessary()530 void MediaProfiles::checkAndAddRequiredProfilesIfNecessary() {
531     if (sIsInitialized) {
532         return;
533     }
534 
535     initRequiredProfileRefs(mCameraIds);
536 
537     for (size_t i = 0, n = mCamcorderProfiles.size(); i < n; ++i) {
538         int product = mCamcorderProfiles[i]->mVideoCodec->mFrameWidth *
539                       mCamcorderProfiles[i]->mVideoCodec->mFrameHeight;
540 
541         camcorder_quality quality = mCamcorderProfiles[i]->mQuality;
542         int cameraId = mCamcorderProfiles[i]->mCameraId;
543         int index = -1;
544         int refIndex = getRequiredProfileRefIndex(cameraId);
545         CHECK(refIndex != -1);
546         RequiredProfileRefInfo *info;
547         camcorder_quality refQuality;
548 
549         // Check high and low from either camcorder profile, timelapse profile
550         // or high speed profile, but not all of them. Default, check camcorder profile
551         size_t j = 0;
552         size_t o = 2;
553         if (isTimelapseProfile(quality)) {
554             // Check timelapse profile instead.
555             j = 2;
556             o = kNumRequiredProfiles;
557         } else if (isHighSpeedProfile(quality)) {
558             // Skip the check for high speed profile.
559             continue;
560         } else {
561             // Must be camcorder profile.
562             CHECK(isCamcorderProfile(quality));
563         }
564         for (; j < o; ++j) {
565             info = &(mRequiredProfileRefs[refIndex].mRefs[j]);
566             if ((j % 2 == 0 && product > info->mResolutionProduct) ||  // low
567                 (j % 2 != 0 && product < info->mResolutionProduct)) {  // high
568                 continue;
569             }
570             switch (j) {
571                 case 0:
572                    refQuality = CAMCORDER_QUALITY_LOW;
573                    break;
574                 case 1:
575                    refQuality = CAMCORDER_QUALITY_HIGH;
576                    break;
577                 case 2:
578                    refQuality = CAMCORDER_QUALITY_TIME_LAPSE_LOW;
579                    break;
580                 case 3:
581                    refQuality = CAMCORDER_QUALITY_TIME_LAPSE_HIGH;
582                    break;
583                 default:
584                     CHECK(!"Should never reach here");
585             }
586 
587             if (!info->mHasRefProfile) {
588                 index = getCamcorderProfileIndex(cameraId, refQuality);
589             }
590             if (index == -1) {
591                 // New high or low quality profile is found.
592                 // Update its reference.
593                 info->mHasRefProfile = true;
594                 info->mRefProfileIndex = i;
595                 info->mResolutionProduct = product;
596             }
597         }
598     }
599 
600     for (size_t cameraId = 0; cameraId < mCameraIds.size(); ++cameraId) {
601         for (size_t j = 0; j < kNumRequiredProfiles; ++j) {
602             int refIndex = getRequiredProfileRefIndex(cameraId);
603             CHECK(refIndex != -1);
604             RequiredProfileRefInfo *info =
605                     &mRequiredProfileRefs[refIndex].mRefs[j];
606 
607             if (info->mHasRefProfile) {
608 
609                 std::unique_ptr<CamcorderProfile> profile =
610                     std::make_unique<CamcorderProfile>(
611                             *mCamcorderProfiles[info->mRefProfileIndex]);
612 
613                 // Overwrite the quality
614                 switch (j % kNumRequiredProfiles) {
615                     case 0:
616                         profile->mQuality = CAMCORDER_QUALITY_LOW;
617                         break;
618                     case 1:
619                         profile->mQuality = CAMCORDER_QUALITY_HIGH;
620                         break;
621                     case 2:
622                         profile->mQuality = CAMCORDER_QUALITY_TIME_LAPSE_LOW;
623                         break;
624                     case 3:
625                         profile->mQuality = CAMCORDER_QUALITY_TIME_LAPSE_HIGH;
626                         break;
627                     default:
628                         CHECK(!"Should never come here");
629                 }
630 
631                 int index = getCamcorderProfileIndex(cameraId, profile->mQuality);
632                 if (index != -1) {
633                     ALOGV("Profile quality %d for camera %zu already exists",
634                         profile->mQuality, cameraId);
635                     CHECK(index == refIndex);
636                     continue;
637                 }
638 
639                 // Insert the new profile
640                 ALOGV("Add a profile: quality %d=>%d for camera %zu",
641                         mCamcorderProfiles[info->mRefProfileIndex]->mQuality,
642                         profile->mQuality, cameraId);
643 
644                 mCamcorderProfiles.add(profile.release());
645             }
646         }
647     }
648 }
649 
650 /*static*/ MediaProfiles*
getInstance()651 MediaProfiles::getInstance()
652 {
653     ALOGV("getInstance");
654     Mutex::Autolock lock(sLock);
655     if (!sIsInitialized) {
656         char value[PROPERTY_VALUE_MAX];
657         if (property_get("media.settings.xml", value, NULL) <= 0) {
658             const char* xmlFile = nullptr;
659             for (auto const& f : getXmlPaths()) {
660                 if (checkXmlFile(f)) {
661                     xmlFile = f;
662                     break;
663                 }
664             }
665             if (xmlFile == nullptr) {
666                 ALOGW("Could not find a validated xml file. "
667                         "Using the default instance instead.");
668                 sInstance = createDefaultInstance();
669             } else {
670                 sInstance = createInstanceFromXmlFile(xmlFile);
671             }
672         } else {
673             sInstance = createInstanceFromXmlFile(value);
674         }
675         CHECK(sInstance != NULL);
676         sInstance->checkAndAddRequiredProfilesIfNecessary();
677         sIsInitialized = true;
678     }
679 
680     return sInstance;
681 }
682 
683 /*static*/ MediaProfiles::VideoEncoderCap*
createDefaultH263VideoEncoderCap()684 MediaProfiles::createDefaultH263VideoEncoderCap()
685 {
686     return new MediaProfiles::VideoEncoderCap(
687         VIDEO_ENCODER_H263, 192000, 420000, 176, 352, 144, 288, 1, 20);
688 }
689 
690 /*static*/ MediaProfiles::VideoEncoderCap*
createDefaultM4vVideoEncoderCap()691 MediaProfiles::createDefaultM4vVideoEncoderCap()
692 {
693     return new MediaProfiles::VideoEncoderCap(
694         VIDEO_ENCODER_MPEG_4_SP, 192000, 420000, 176, 352, 144, 288, 1, 20);
695 }
696 
697 
698 /*static*/ void
createDefaultVideoEncoders(MediaProfiles * profiles)699 MediaProfiles::createDefaultVideoEncoders(MediaProfiles *profiles)
700 {
701     profiles->mVideoEncoders.add(createDefaultH263VideoEncoderCap());
702     profiles->mVideoEncoders.add(createDefaultM4vVideoEncoderCap());
703 }
704 
705 /*static*/ MediaProfiles::CamcorderProfile*
createDefaultCamcorderTimeLapseQcifProfile(camcorder_quality quality)706 MediaProfiles::createDefaultCamcorderTimeLapseQcifProfile(camcorder_quality quality)
707 {
708     MediaProfiles::VideoCodec *videoCodec =
709         new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 1000000, 176, 144, 20);
710 
711     AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
712     CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
713     profile->mCameraId = 0;
714     profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
715     profile->mQuality = quality;
716     profile->mDuration = 60;
717     profile->mVideoCodec = videoCodec;
718     profile->mAudioCodec = audioCodec;
719     return profile;
720 }
721 
722 /*static*/ MediaProfiles::CamcorderProfile*
createDefaultCamcorderTimeLapse480pProfile(camcorder_quality quality)723 MediaProfiles::createDefaultCamcorderTimeLapse480pProfile(camcorder_quality quality)
724 {
725     MediaProfiles::VideoCodec *videoCodec =
726         new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 20000000, 720, 480, 20);
727 
728     AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
729     CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
730     profile->mCameraId = 0;
731     profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
732     profile->mQuality = quality;
733     profile->mDuration = 60;
734     profile->mVideoCodec = videoCodec;
735     profile->mAudioCodec = audioCodec;
736     return profile;
737 }
738 
739 /*static*/ void
createDefaultCamcorderTimeLapseLowProfiles(MediaProfiles::CamcorderProfile ** lowTimeLapseProfile,MediaProfiles::CamcorderProfile ** lowSpecificTimeLapseProfile)740 MediaProfiles::createDefaultCamcorderTimeLapseLowProfiles(
741         MediaProfiles::CamcorderProfile **lowTimeLapseProfile,
742         MediaProfiles::CamcorderProfile **lowSpecificTimeLapseProfile) {
743     *lowTimeLapseProfile = createDefaultCamcorderTimeLapseQcifProfile(
744             CAMCORDER_QUALITY_TIME_LAPSE_LOW);
745     *lowSpecificTimeLapseProfile = createDefaultCamcorderTimeLapseQcifProfile(
746             CAMCORDER_QUALITY_TIME_LAPSE_QCIF);
747 }
748 
749 /*static*/ void
createDefaultCamcorderTimeLapseHighProfiles(MediaProfiles::CamcorderProfile ** highTimeLapseProfile,MediaProfiles::CamcorderProfile ** highSpecificTimeLapseProfile)750 MediaProfiles::createDefaultCamcorderTimeLapseHighProfiles(
751         MediaProfiles::CamcorderProfile **highTimeLapseProfile,
752         MediaProfiles::CamcorderProfile **highSpecificTimeLapseProfile) {
753     *highTimeLapseProfile = createDefaultCamcorderTimeLapse480pProfile(
754             CAMCORDER_QUALITY_TIME_LAPSE_HIGH);
755     *highSpecificTimeLapseProfile = createDefaultCamcorderTimeLapse480pProfile(
756             CAMCORDER_QUALITY_TIME_LAPSE_480P);
757 }
758 
759 /*static*/ MediaProfiles::CamcorderProfile*
createDefaultCamcorderQcifProfile(camcorder_quality quality)760 MediaProfiles::createDefaultCamcorderQcifProfile(camcorder_quality quality)
761 {
762     MediaProfiles::VideoCodec *videoCodec =
763         new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 192000, 176, 144, 20);
764 
765     MediaProfiles::AudioCodec *audioCodec =
766         new MediaProfiles::AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
767 
768     MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
769     profile->mCameraId = 0;
770     profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
771     profile->mQuality = quality;
772     profile->mDuration = 30;
773     profile->mVideoCodec = videoCodec;
774     profile->mAudioCodec = audioCodec;
775     return profile;
776 }
777 
778 /*static*/ MediaProfiles::CamcorderProfile*
createDefaultCamcorderCifProfile(camcorder_quality quality)779 MediaProfiles::createDefaultCamcorderCifProfile(camcorder_quality quality)
780 {
781     MediaProfiles::VideoCodec *videoCodec =
782         new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 360000, 352, 288, 20);
783 
784     AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
785     CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
786     profile->mCameraId = 0;
787     profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
788     profile->mQuality = quality;
789     profile->mDuration = 60;
790     profile->mVideoCodec = videoCodec;
791     profile->mAudioCodec = audioCodec;
792     return profile;
793 }
794 
795 /*static*/ void
createDefaultCamcorderLowProfiles(MediaProfiles::CamcorderProfile ** lowProfile,MediaProfiles::CamcorderProfile ** lowSpecificProfile)796 MediaProfiles::createDefaultCamcorderLowProfiles(
797         MediaProfiles::CamcorderProfile **lowProfile,
798         MediaProfiles::CamcorderProfile **lowSpecificProfile) {
799     *lowProfile = createDefaultCamcorderQcifProfile(CAMCORDER_QUALITY_LOW);
800     *lowSpecificProfile = createDefaultCamcorderQcifProfile(CAMCORDER_QUALITY_QCIF);
801 }
802 
803 /*static*/ void
createDefaultCamcorderHighProfiles(MediaProfiles::CamcorderProfile ** highProfile,MediaProfiles::CamcorderProfile ** highSpecificProfile)804 MediaProfiles::createDefaultCamcorderHighProfiles(
805         MediaProfiles::CamcorderProfile **highProfile,
806         MediaProfiles::CamcorderProfile **highSpecificProfile) {
807     *highProfile = createDefaultCamcorderCifProfile(CAMCORDER_QUALITY_HIGH);
808     *highSpecificProfile = createDefaultCamcorderCifProfile(CAMCORDER_QUALITY_CIF);
809 }
810 
811 /*static*/ void
createDefaultCamcorderProfiles(MediaProfiles * profiles)812 MediaProfiles::createDefaultCamcorderProfiles(MediaProfiles *profiles)
813 {
814     // low camcorder profiles.
815     MediaProfiles::CamcorderProfile *lowProfile, *lowSpecificProfile;
816     createDefaultCamcorderLowProfiles(&lowProfile, &lowSpecificProfile);
817     profiles->mCamcorderProfiles.add(lowProfile);
818     profiles->mCamcorderProfiles.add(lowSpecificProfile);
819 
820     // high camcorder profiles.
821     MediaProfiles::CamcorderProfile* highProfile, *highSpecificProfile;
822     createDefaultCamcorderHighProfiles(&highProfile, &highSpecificProfile);
823     profiles->mCamcorderProfiles.add(highProfile);
824     profiles->mCamcorderProfiles.add(highSpecificProfile);
825 
826     // low camcorder time lapse profiles.
827     MediaProfiles::CamcorderProfile *lowTimeLapseProfile, *lowSpecificTimeLapseProfile;
828     createDefaultCamcorderTimeLapseLowProfiles(&lowTimeLapseProfile, &lowSpecificTimeLapseProfile);
829     profiles->mCamcorderProfiles.add(lowTimeLapseProfile);
830     profiles->mCamcorderProfiles.add(lowSpecificTimeLapseProfile);
831 
832     // high camcorder time lapse profiles.
833     MediaProfiles::CamcorderProfile *highTimeLapseProfile, *highSpecificTimeLapseProfile;
834     createDefaultCamcorderTimeLapseHighProfiles(&highTimeLapseProfile,
835             &highSpecificTimeLapseProfile);
836     profiles->mCamcorderProfiles.add(highTimeLapseProfile);
837     profiles->mCamcorderProfiles.add(highSpecificTimeLapseProfile);
838 
839     // For emulator and other legacy devices which does not have a
840     // media_profiles.xml file, We assume that the default camera id
841     // is 0 and that is the only camera available.
842     profiles->mCameraIds.push(0);
843 }
844 
845 /*static*/ void
createDefaultAudioEncoders(MediaProfiles * profiles)846 MediaProfiles::createDefaultAudioEncoders(MediaProfiles *profiles)
847 {
848     profiles->mAudioEncoders.add(createDefaultAmrNBEncoderCap());
849 }
850 
851 /*static*/ void
createDefaultVideoDecoders(MediaProfiles * profiles)852 MediaProfiles::createDefaultVideoDecoders(MediaProfiles *profiles)
853 {
854     MediaProfiles::VideoDecoderCap *cap =
855         new MediaProfiles::VideoDecoderCap(VIDEO_DECODER_WMV);
856 
857     profiles->mVideoDecoders.add(cap);
858 }
859 
860 /*static*/ void
createDefaultAudioDecoders(MediaProfiles * profiles)861 MediaProfiles::createDefaultAudioDecoders(MediaProfiles *profiles)
862 {
863     MediaProfiles::AudioDecoderCap *cap =
864         new MediaProfiles::AudioDecoderCap(AUDIO_DECODER_WMA);
865 
866     profiles->mAudioDecoders.add(cap);
867 }
868 
869 /*static*/ void
createDefaultEncoderOutputFileFormats(MediaProfiles * profiles)870 MediaProfiles::createDefaultEncoderOutputFileFormats(MediaProfiles *profiles)
871 {
872     profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_THREE_GPP);
873     profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_MPEG_4);
874 }
875 
876 /*static*/ MediaProfiles::AudioEncoderCap*
createDefaultAmrNBEncoderCap()877 MediaProfiles::createDefaultAmrNBEncoderCap()
878 {
879     return new MediaProfiles::AudioEncoderCap(
880         AUDIO_ENCODER_AMR_NB, 5525, 12200, 8000, 8000, 1, 1);
881 }
882 
883 /*static*/ void
createDefaultImageEncodingQualityLevels(MediaProfiles * profiles)884 MediaProfiles::createDefaultImageEncodingQualityLevels(MediaProfiles *profiles)
885 {
886     ImageEncodingQualityLevels *levels = new ImageEncodingQualityLevels();
887     levels->mCameraId = 0;
888     levels->mLevels.add(70);
889     levels->mLevels.add(80);
890     levels->mLevels.add(90);
891     profiles->mImageEncodingQualityLevels.add(levels);
892 }
893 
894 /*static*/ MediaProfiles*
createDefaultInstance()895 MediaProfiles::createDefaultInstance()
896 {
897     MediaProfiles *profiles = new MediaProfiles;
898     createDefaultCamcorderProfiles(profiles);
899     createDefaultVideoEncoders(profiles);
900     createDefaultAudioEncoders(profiles);
901     createDefaultVideoDecoders(profiles);
902     createDefaultAudioDecoders(profiles);
903     createDefaultEncoderOutputFileFormats(profiles);
904     createDefaultImageEncodingQualityLevels(profiles);
905     return profiles;
906 }
907 
checkXmlFile(const char * xmlFile)908 bool MediaProfiles::checkXmlFile(const char* xmlFile) {
909     struct stat fStat;
910     return stat(xmlFile, &fStat) == 0 && S_ISREG(fStat.st_mode);
911     // TODO: Add validation
912 }
913 
914 /*static*/ MediaProfiles*
createInstanceFromXmlFile(const char * xml)915 MediaProfiles::createInstanceFromXmlFile(const char *xml)
916 {
917     FILE *fp = NULL;
918     CHECK((fp = fopen(xml, "r")));
919 
920     XML_Parser parser = ::XML_ParserCreate(NULL);
921     CHECK(parser != NULL);
922 
923     MediaProfiles *profiles = new MediaProfiles();
924     ::XML_SetUserData(parser, profiles);
925     ::XML_SetElementHandler(parser, startElementHandler, NULL);
926 
927     /*
928       FIXME:
929       expat is not compiled with -DXML_DTD. We don't have DTD parsing support.
930 
931       if (!::XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS)) {
932           ALOGE("failed to enable DTD support in the xml file");
933           return UNKNOWN_ERROR;
934       }
935 
936     */
937 
938     const int BUFF_SIZE = 512;
939     for (;;) {
940         void *buff = ::XML_GetBuffer(parser, BUFF_SIZE);
941         if (buff == NULL) {
942             ALOGE("failed to in call to XML_GetBuffer()");
943             delete profiles;
944             profiles = NULL;
945             goto exit;
946         }
947 
948         int bytes_read = ::fread(buff, 1, BUFF_SIZE, fp);
949         if (bytes_read < 0) {
950             ALOGE("failed in call to read");
951             delete profiles;
952             profiles = NULL;
953             goto exit;
954         }
955 
956         CHECK(::XML_ParseBuffer(parser, bytes_read, bytes_read == 0));
957 
958         if (bytes_read == 0) break;  // done parsing the xml file
959     }
960 
961 exit:
962     ::XML_ParserFree(parser);
963     ::fclose(fp);
964     return profiles;
965 }
966 
getOutputFileFormats() const967 Vector<output_format> MediaProfiles::getOutputFileFormats() const
968 {
969     return mEncoderOutputFileFormats;  // copy out
970 }
971 
getVideoEncoders() const972 Vector<video_encoder> MediaProfiles::getVideoEncoders() const
973 {
974     Vector<video_encoder> encoders;
975     for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
976         encoders.add(mVideoEncoders[i]->mCodec);
977     }
978     return encoders;  // copy out
979 }
980 
getVideoEncoderParamByName(const char * name,video_encoder codec) const981 int MediaProfiles::getVideoEncoderParamByName(const char *name, video_encoder codec) const
982 {
983     ALOGV("getVideoEncoderParamByName: %s for codec %d", name, codec);
984     int index = -1;
985     for (size_t i = 0, n = mVideoEncoders.size(); i < n; ++i) {
986         if (mVideoEncoders[i]->mCodec == codec) {
987             index = i;
988             break;
989         }
990     }
991     if (index == -1) {
992         ALOGE("The given video encoder %d is not found", codec);
993         return -1;
994     }
995 
996     if (!strcmp("enc.vid.width.min", name)) return mVideoEncoders[index]->mMinFrameWidth;
997     if (!strcmp("enc.vid.width.max", name)) return mVideoEncoders[index]->mMaxFrameWidth;
998     if (!strcmp("enc.vid.height.min", name)) return mVideoEncoders[index]->mMinFrameHeight;
999     if (!strcmp("enc.vid.height.max", name)) return mVideoEncoders[index]->mMaxFrameHeight;
1000     if (!strcmp("enc.vid.bps.min", name)) return mVideoEncoders[index]->mMinBitRate;
1001     if (!strcmp("enc.vid.bps.max", name)) return mVideoEncoders[index]->mMaxBitRate;
1002     if (!strcmp("enc.vid.fps.min", name)) return mVideoEncoders[index]->mMinFrameRate;
1003     if (!strcmp("enc.vid.fps.max", name)) return mVideoEncoders[index]->mMaxFrameRate;
1004 
1005     ALOGE("The given video encoder param name %s is not found", name);
1006     return -1;
1007 }
1008 
getAudioEncoders() const1009 Vector<audio_encoder> MediaProfiles::getAudioEncoders() const
1010 {
1011     Vector<audio_encoder> encoders;
1012     for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
1013         encoders.add(mAudioEncoders[i]->mCodec);
1014     }
1015     return encoders;  // copy out
1016 }
1017 
getAudioEncoderParamByName(const char * name,audio_encoder codec) const1018 int MediaProfiles::getAudioEncoderParamByName(const char *name, audio_encoder codec) const
1019 {
1020     ALOGV("getAudioEncoderParamByName: %s for codec %d", name, codec);
1021     int index = -1;
1022     for (size_t i = 0, n = mAudioEncoders.size(); i < n; ++i) {
1023         if (mAudioEncoders[i]->mCodec == codec) {
1024             index = i;
1025             break;
1026         }
1027     }
1028     if (index == -1) {
1029         ALOGE("The given audio encoder %d is not found", codec);
1030         return -1;
1031     }
1032 
1033     if (!strcmp("enc.aud.ch.min", name)) return mAudioEncoders[index]->mMinChannels;
1034     if (!strcmp("enc.aud.ch.max", name)) return mAudioEncoders[index]->mMaxChannels;
1035     if (!strcmp("enc.aud.bps.min", name)) return mAudioEncoders[index]->mMinBitRate;
1036     if (!strcmp("enc.aud.bps.max", name)) return mAudioEncoders[index]->mMaxBitRate;
1037     if (!strcmp("enc.aud.hz.min", name)) return mAudioEncoders[index]->mMinSampleRate;
1038     if (!strcmp("enc.aud.hz.max", name)) return mAudioEncoders[index]->mMaxSampleRate;
1039 
1040     ALOGE("The given audio encoder param name %s is not found", name);
1041     return -1;
1042 }
1043 
getVideoDecoders() const1044 Vector<video_decoder> MediaProfiles::getVideoDecoders() const
1045 {
1046     Vector<video_decoder> decoders;
1047     for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
1048         decoders.add(mVideoDecoders[i]->mCodec);
1049     }
1050     return decoders;  // copy out
1051 }
1052 
getAudioDecoders() const1053 Vector<audio_decoder> MediaProfiles::getAudioDecoders() const
1054 {
1055     Vector<audio_decoder> decoders;
1056     for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
1057         decoders.add(mAudioDecoders[i]->mCodec);
1058     }
1059     return decoders;  // copy out
1060 }
1061 
getCamcorderProfileIndex(int cameraId,camcorder_quality quality) const1062 int MediaProfiles::getCamcorderProfileIndex(int cameraId, camcorder_quality quality) const
1063 {
1064     int index = -1;
1065     for (size_t i = 0, n = mCamcorderProfiles.size(); i < n; ++i) {
1066         if (mCamcorderProfiles[i]->mCameraId == cameraId &&
1067             mCamcorderProfiles[i]->mQuality == quality) {
1068             index = i;
1069             break;
1070         }
1071     }
1072     return index;
1073 }
1074 
getCamcorderProfileParamByName(const char * name,int cameraId,camcorder_quality quality) const1075 int MediaProfiles::getCamcorderProfileParamByName(const char *name,
1076                                                   int cameraId,
1077                                                   camcorder_quality quality) const
1078 {
1079     ALOGV("getCamcorderProfileParamByName: %s for camera %d, quality %d",
1080         name, cameraId, quality);
1081 
1082     int index = getCamcorderProfileIndex(cameraId, quality);
1083     if (index == -1) {
1084         ALOGE("The given camcorder profile camera %d quality %d is not found",
1085             cameraId, quality);
1086         return -1;
1087     }
1088 
1089     if (!strcmp("duration", name)) return mCamcorderProfiles[index]->mDuration;
1090     if (!strcmp("file.format", name)) return mCamcorderProfiles[index]->mFileFormat;
1091     if (!strcmp("vid.codec", name)) return mCamcorderProfiles[index]->mVideoCodec->mCodec;
1092     if (!strcmp("vid.width", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameWidth;
1093     if (!strcmp("vid.height", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameHeight;
1094     if (!strcmp("vid.bps", name)) return mCamcorderProfiles[index]->mVideoCodec->mBitRate;
1095     if (!strcmp("vid.fps", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameRate;
1096     if (!strcmp("aud.codec", name)) return mCamcorderProfiles[index]->mAudioCodec->mCodec;
1097     if (!strcmp("aud.bps", name)) return mCamcorderProfiles[index]->mAudioCodec->mBitRate;
1098     if (!strcmp("aud.ch", name)) return mCamcorderProfiles[index]->mAudioCodec->mChannels;
1099     if (!strcmp("aud.hz", name)) return mCamcorderProfiles[index]->mAudioCodec->mSampleRate;
1100 
1101     ALOGE("The given camcorder profile param id %d name %s is not found", cameraId, name);
1102     return -1;
1103 }
1104 
hasCamcorderProfile(int cameraId,camcorder_quality quality) const1105 bool MediaProfiles::hasCamcorderProfile(int cameraId, camcorder_quality quality) const
1106 {
1107     return (getCamcorderProfileIndex(cameraId, quality) != -1);
1108 }
1109 
getImageEncodingQualityLevels(int cameraId) const1110 Vector<int> MediaProfiles::getImageEncodingQualityLevels(int cameraId) const
1111 {
1112     Vector<int> result;
1113     ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
1114     if (levels != NULL) {
1115         result = levels->mLevels;  // copy out
1116     }
1117     return result;
1118 }
1119 
getStartTimeOffsetMs(int cameraId) const1120 int MediaProfiles::getStartTimeOffsetMs(int cameraId) const {
1121     int offsetTimeMs = -1;
1122     ssize_t index = mStartTimeOffsets.indexOfKey(cameraId);
1123     if (index >= 0) {
1124         offsetTimeMs = mStartTimeOffsets.valueFor(cameraId);
1125     }
1126     ALOGV("offsetTime=%d ms and cameraId=%d", offsetTimeMs, cameraId);
1127     return offsetTimeMs;
1128 }
1129 
~MediaProfiles()1130 MediaProfiles::~MediaProfiles()
1131 {
1132     CHECK("destructor should never be called" == 0);
1133 #if 0
1134     for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
1135         delete mAudioEncoders[i];
1136     }
1137     mAudioEncoders.clear();
1138 
1139     for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
1140         delete mVideoEncoders[i];
1141     }
1142     mVideoEncoders.clear();
1143 
1144     for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
1145         delete mVideoDecoders[i];
1146     }
1147     mVideoDecoders.clear();
1148 
1149     for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
1150         delete mAudioDecoders[i];
1151     }
1152     mAudioDecoders.clear();
1153 
1154     for (size_t i = 0; i < mCamcorderProfiles.size(); ++i) {
1155         delete mCamcorderProfiles[i];
1156     }
1157     mCamcorderProfiles.clear();
1158 #endif
1159 }
1160 } // namespace android
1161