blob: 1be82d8a79ce8fc76605a1e8ca5be6815f4bb6b3 [file] [log] [blame]
James Dong1d7491b2010-01-19 17:45:38 -08001/*
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>
Elliott Hughes242b4002015-07-10 11:09:23 -070026#include <expat.h>
James Dong1d7491b2010-01-19 17:45:38 -080027#include <media/MediaProfiles.h>
James Dongf1d5aa12012-02-06 23:46:37 -080028#include <media/stagefright/foundation/ADebug.h>
James Dong6c6b4d02012-03-12 14:37:53 -070029#include <OMX_Video.h>
Pawin Vongmasad7db05b2017-05-03 04:19:20 -070030#include <sys/stat.h>
James Dong1d7491b2010-01-19 17:45:38 -080031
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -080032#include <array>
33#include <string>
34#include <vector>
35
James Dong1d7491b2010-01-19 17:45:38 -080036namespace android {
37
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -080038namespace /* unnamed */ {
39
40// Returns a list of possible paths for the media_profiles XML file.
41std::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,
Matthias Springer0f44db02020-08-14 14:32:05 +090066 "system/etc/media_profiles.xml" // System fallback
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -080067 };
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
James Dong1d7491b2010-01-19 17:45:38 -080081Mutex MediaProfiles::sLock;
82bool MediaProfiles::sIsInitialized = false;
83MediaProfiles *MediaProfiles::sInstance = NULL;
84
85const MediaProfiles::NameToTagMap MediaProfiles::sVideoEncoderNameMap[] = {
86 {"h263", VIDEO_ENCODER_H263},
87 {"h264", VIDEO_ENCODER_H264},
Wonsik Kim9aa87d42015-12-07 13:52:02 +090088 {"m4v", VIDEO_ENCODER_MPEG_4_SP},
89 {"hevc", VIDEO_ENCODER_HEVC}
James Dong1d7491b2010-01-19 17:45:38 -080090};
91
92const MediaProfiles::NameToTagMap MediaProfiles::sAudioEncoderNameMap[] = {
Dave Burkeaeb8fd42012-04-19 00:14:27 -070093 {"amrnb", AUDIO_ENCODER_AMR_NB},
94 {"amrwb", AUDIO_ENCODER_AMR_WB},
95 {"aac", AUDIO_ENCODER_AAC},
Dave Burkef60c6602012-04-28 21:58:22 -070096 {"heaac", AUDIO_ENCODER_HE_AAC},
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -080097 {"aaceld", AUDIO_ENCODER_AAC_ELD},
Ray Essickdf27b042018-11-27 18:55:09 -080098 {"opus", AUDIO_ENCODER_OPUS}
James Dong1d7491b2010-01-19 17:45:38 -080099};
100
101const MediaProfiles::NameToTagMap MediaProfiles::sFileFormatMap[] = {
102 {"3gp", OUTPUT_FORMAT_THREE_GPP},
103 {"mp4", OUTPUT_FORMAT_MPEG_4}
104};
105
106const MediaProfiles::NameToTagMap MediaProfiles::sVideoDecoderNameMap[] = {
107 {"wmv", VIDEO_DECODER_WMV}
108};
109
110const MediaProfiles::NameToTagMap MediaProfiles::sAudioDecoderNameMap[] = {
111 {"wma", AUDIO_DECODER_WMA}
112};
113
114const MediaProfiles::NameToTagMap MediaProfiles::sCamcorderQualityNameMap[] = {
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700115 {"low", CAMCORDER_QUALITY_LOW},
James Dong1d7491b2010-01-19 17:45:38 -0800116 {"high", CAMCORDER_QUALITY_HIGH},
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700117 {"qcif", CAMCORDER_QUALITY_QCIF},
Nipun Kwatra9783ed82010-09-10 15:45:57 -0700118 {"cif", CAMCORDER_QUALITY_CIF},
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700119 {"480p", CAMCORDER_QUALITY_480P},
120 {"720p", CAMCORDER_QUALITY_720P},
121 {"1080p", CAMCORDER_QUALITY_1080P},
Zhijun He5f6af1a2014-06-10 08:26:33 -0700122 {"2160p", CAMCORDER_QUALITY_2160P},
James Dong669012d2011-09-19 16:27:31 -0700123 {"qvga", CAMCORDER_QUALITY_QVGA},
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700124
125 {"timelapselow", CAMCORDER_QUALITY_TIME_LAPSE_LOW},
126 {"timelapsehigh", CAMCORDER_QUALITY_TIME_LAPSE_HIGH},
127 {"timelapseqcif", CAMCORDER_QUALITY_TIME_LAPSE_QCIF},
Nipun Kwatra9783ed82010-09-10 15:45:57 -0700128 {"timelapsecif", CAMCORDER_QUALITY_TIME_LAPSE_CIF},
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700129 {"timelapse480p", CAMCORDER_QUALITY_TIME_LAPSE_480P},
130 {"timelapse720p", CAMCORDER_QUALITY_TIME_LAPSE_720P},
James Dong669012d2011-09-19 16:27:31 -0700131 {"timelapse1080p", CAMCORDER_QUALITY_TIME_LAPSE_1080P},
Zhijun He5f6af1a2014-06-10 08:26:33 -0700132 {"timelapse2160p", CAMCORDER_QUALITY_TIME_LAPSE_2160P},
James Dong669012d2011-09-19 16:27:31 -0700133 {"timelapseqvga", CAMCORDER_QUALITY_TIME_LAPSE_QVGA},
Zhijun Hee0790972014-07-23 15:17:26 -0700134
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},
Zhijun He9520aa62014-09-09 16:18:31 -0700140 {"highspeed2160p", CAMCORDER_QUALITY_HIGH_SPEED_2160P},
Praveen Chavanb5bfa0e2019-01-16 15:49:42 -0800141
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},
James Dong1d7491b2010-01-19 17:45:38 -0800154};
155
Glenn Kasten80520382014-01-31 16:49:31 -0800156#if LOG_NDEBUG
157#define UNUSED __unused
158#else
159#define UNUSED
160#endif
161
James Dong1d7491b2010-01-19 17:45:38 -0800162/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800163MediaProfiles::logVideoCodec(const MediaProfiles::VideoCodec& codec UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800164{
Steve Block3856b092011-10-20 11:56:00 +0100165 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);
James Dong1d7491b2010-01-19 17:45:38 -0800171}
172
173/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800174MediaProfiles::logAudioCodec(const MediaProfiles::AudioCodec& codec UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800175{
Steve Block3856b092011-10-20 11:56:00 +0100176 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);
James Dong1d7491b2010-01-19 17:45:38 -0800181}
182
183/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800184MediaProfiles::logVideoEncoderCap(const MediaProfiles::VideoEncoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800185{
Steve Block3856b092011-10-20 11:56:00 +0100186 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);
James Dong1d7491b2010-01-19 17:45:38 -0800192}
193
194/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800195MediaProfiles::logAudioEncoderCap(const MediaProfiles::AudioEncoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800196{
Steve Block3856b092011-10-20 11:56:00 +0100197 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);
James Dong1d7491b2010-01-19 17:45:38 -0800202}
203
204/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800205MediaProfiles::logVideoDecoderCap(const MediaProfiles::VideoDecoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800206{
Steve Block3856b092011-10-20 11:56:00 +0100207 ALOGV("video decoder cap:");
208 ALOGV("codec = %d", cap.mCodec);
James Dong1d7491b2010-01-19 17:45:38 -0800209}
210
211/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800212MediaProfiles::logAudioDecoderCap(const MediaProfiles::AudioDecoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800213{
Steve Block3856b092011-10-20 11:56:00 +0100214 ALOGV("audio codec cap:");
215 ALOGV("codec = %d", cap.mCodec);
James Dong1d7491b2010-01-19 17:45:38 -0800216}
217
218/*static*/ int
Glenn Kastenb187de12014-12-30 08:18:15 -0800219MediaProfiles::findTagForName(const MediaProfiles::NameToTagMap *map, size_t nMappings,
220 const char *name)
James Dong1d7491b2010-01-19 17:45:38 -0800221{
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*
233MediaProfiles::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*
257MediaProfiles::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*
278MediaProfiles::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*
294MediaProfiles::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*
310MediaProfiles::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*
336MediaProfiles::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 =
Glenn Kastenb187de12014-12-30 08:18:15 -0800352 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]));
James Dong1d7491b2010-01-19 17:45:38 -0800354 logAudioEncoderCap(*cap);
355 return cap;
356}
357
358/*static*/ output_format
359MediaProfiles::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
James Dong2a7e0a12011-02-28 21:07:39 -0800370static 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
James Dong1d7491b2010-01-19 17:45:38 -0800379/*static*/ MediaProfiles::CamcorderProfile*
James Dong2a7e0a12011-02-28 21:07:39 -0800380MediaProfiles::createCamcorderProfile(int cameraId, const char **atts, Vector<int>& cameraIds)
James Dong1d7491b2010-01-19 17:45:38 -0800381{
382 CHECK(!strcmp("quality", atts[0]) &&
383 !strcmp("fileFormat", atts[2]) &&
384 !strcmp("duration", atts[4]));
385
Glenn Kastenb187de12014-12-30 08:18:15 -0800386 const size_t nProfileMappings = sizeof(sCamcorderQualityNameMap)/
387 sizeof(sCamcorderQualityNameMap[0]);
James Dong1d7491b2010-01-19 17:45:38 -0800388 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;
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800396 profile->mCameraId = cameraId;
James Dong2a7e0a12011-02-28 21:07:39 -0800397 if (!isCameraIdFound(cameraId, cameraIds)) {
398 cameraIds.add(cameraId);
399 }
James Dong1d7491b2010-01-19 17:45:38 -0800400 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
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800406MediaProfiles::ImageEncodingQualityLevels*
407MediaProfiles::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
419void MediaProfiles::addImageEncodingQualityLevel(int cameraId, const char** atts)
James Dongf5a83852010-02-23 17:21:44 -0800420{
421 CHECK(!strcmp("quality", atts[0]));
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800422 int quality = atoi(atts[1]);
Glenn Kasten90bebef2012-01-27 15:24:38 -0800423 ALOGV("%s: cameraId=%d, quality=%d", __func__, cameraId, quality);
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800424 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
436MediaProfiles::getCameraId(const char** atts)
437{
438 if (!atts[0]) return 0; // default cameraId = 0
439 CHECK(!strcmp("cameraId", atts[0]));
James Dongf5a83852010-02-23 17:21:44 -0800440 return atoi(atts[1]);
441}
442
James Dong0f056292011-05-09 18:49:31 -0700443void MediaProfiles::addStartTimeOffset(int cameraId, const char** atts)
444{
Eric Laurentb1eb1a02012-10-22 17:44:24 -0700445 int offsetTimeMs = 1000;
James Dong0f056292011-05-09 18:49:31 -0700446 if (atts[2]) {
447 CHECK(!strcmp("startOffsetMs", atts[2]));
448 offsetTimeMs = atoi(atts[3]);
449 }
450
Steve Block3856b092011-10-20 11:56:00 +0100451 ALOGV("%s: cameraId=%d, offset=%d ms", __func__, cameraId, offsetTimeMs);
James Dong0f056292011-05-09 18:49:31 -0700452 mStartTimeOffsets.replaceValueFor(cameraId, offsetTimeMs);
453}
Hong Tengcabd5f82011-07-06 18:33:09 -0700454
James Dong1d7491b2010-01-19 17:45:38 -0800455/*static*/ void
456MediaProfiles::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));
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800477 } else if (strcmp("CamcorderProfiles", name) == 0) {
478 profiles->mCurrentCameraId = getCameraId(atts);
James Dong0f056292011-05-09 18:49:31 -0700479 profiles->addStartTimeOffset(profiles->mCurrentCameraId, atts);
James Dong1d7491b2010-01-19 17:45:38 -0800480 } else if (strcmp("EncoderProfile", name) == 0) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800481 profiles->mCamcorderProfiles.add(
James Dong2a7e0a12011-02-28 21:07:39 -0800482 createCamcorderProfile(profiles->mCurrentCameraId, atts, profiles->mCameraIds));
James Dongf5a83852010-02-23 17:21:44 -0800483 } else if (strcmp("ImageEncoding", name) == 0) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800484 profiles->addImageEncodingQualityLevel(profiles->mCurrentCameraId, atts);
James Dong1d7491b2010-01-19 17:45:38 -0800485 }
486}
487
James Dong2a7e0a12011-02-28 21:07:39 -0800488static bool isCamcorderProfile(camcorder_quality quality) {
489 return quality >= CAMCORDER_QUALITY_LIST_START &&
490 quality <= CAMCORDER_QUALITY_LIST_END;
491}
492
493static bool isTimelapseProfile(camcorder_quality quality) {
494 return quality >= CAMCORDER_QUALITY_TIME_LAPSE_LIST_START &&
495 quality <= CAMCORDER_QUALITY_TIME_LAPSE_LIST_END;
496}
497
Zhijun Hee0790972014-07-23 15:17:26 -0700498static bool isHighSpeedProfile(camcorder_quality quality) {
499 return quality >= CAMCORDER_QUALITY_HIGH_SPEED_LIST_START &&
500 quality <= CAMCORDER_QUALITY_HIGH_SPEED_LIST_END;
501}
502
James Dong2a7e0a12011-02-28 21:07:39 -0800503void MediaProfiles::initRequiredProfileRefs(const Vector<int>& cameraIds) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700504 ALOGV("Number of camera ids: %zu", cameraIds.size());
James Dong2a7e0a12011-02-28 21:07:39 -0800505 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
521int 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
530void 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;
James Dong2a7e0a12011-02-28 21:07:39 -0800548
Zhijun Hee0790972014-07-23 15:17:26 -0700549 // Check high and low from either camcorder profile, timelapse profile
550 // or high speed profile, but not all of them. Default, check camcorder profile
James Dong2a7e0a12011-02-28 21:07:39 -0800551 size_t j = 0;
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200552 size_t o = 2;
James Dong2a7e0a12011-02-28 21:07:39 -0800553 if (isTimelapseProfile(quality)) {
554 // Check timelapse profile instead.
555 j = 2;
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200556 o = kNumRequiredProfiles;
Zhijun Hee0790972014-07-23 15:17:26 -0700557 } else if (isHighSpeedProfile(quality)) {
558 // Skip the check for high speed profile.
559 continue;
James Dong2a7e0a12011-02-28 21:07:39 -0800560 } else {
561 // Must be camcorder profile.
562 CHECK(isCamcorderProfile(quality));
563 }
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200564 for (; j < o; ++j) {
James Dong2a7e0a12011-02-28 21:07:39 -0800565 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
George Burgess IV215545b2018-07-25 10:06:30 -0700609 std::unique_ptr<CamcorderProfile> profile =
610 std::make_unique<CamcorderProfile>(
James Dong2a7e0a12011-02-28 21:07:39 -0800611 *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) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700633 ALOGV("Profile quality %d for camera %zu already exists",
James Dong2a7e0a12011-02-28 21:07:39 -0800634 profile->mQuality, cameraId);
635 CHECK(index == refIndex);
636 continue;
637 }
638
639 // Insert the new profile
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700640 ALOGV("Add a profile: quality %d=>%d for camera %zu",
James Dong2a7e0a12011-02-28 21:07:39 -0800641 mCamcorderProfiles[info->mRefProfileIndex]->mQuality,
642 profile->mQuality, cameraId);
643
George Burgess IV215545b2018-07-25 10:06:30 -0700644 mCamcorderProfiles.add(profile.release());
James Dong2a7e0a12011-02-28 21:07:39 -0800645 }
646 }
647 }
648}
649
James Dong1d7491b2010-01-19 17:45:38 -0800650/*static*/ MediaProfiles*
651MediaProfiles::getInstance()
652{
Steve Block3856b092011-10-20 11:56:00 +0100653 ALOGV("getInstance");
James Dong1d7491b2010-01-19 17:45:38 -0800654 Mutex::Autolock lock(sLock);
655 if (!sIsInitialized) {
656 char value[PROPERTY_VALUE_MAX];
657 if (property_get("media.settings.xml", value, NULL) <= 0) {
Pawin Vongmasad7db05b2017-05-03 04:19:20 -0700658 const char* xmlFile = nullptr;
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -0800659 for (auto const& f : getXmlPaths()) {
Pawin Vongmasad7db05b2017-05-03 04:19:20 -0700660 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.");
James Dong1d7491b2010-01-19 17:45:38 -0800668 sInstance = createDefaultInstance();
669 } else {
Pawin Vongmasad7db05b2017-05-03 04:19:20 -0700670 sInstance = createInstanceFromXmlFile(xmlFile);
James Dong1d7491b2010-01-19 17:45:38 -0800671 }
672 } else {
673 sInstance = createInstanceFromXmlFile(value);
674 }
James Dong2a7e0a12011-02-28 21:07:39 -0800675 CHECK(sInstance != NULL);
676 sInstance->checkAndAddRequiredProfilesIfNecessary();
677 sIsInitialized = true;
James Dong1d7491b2010-01-19 17:45:38 -0800678 }
679
680 return sInstance;
681}
682
683/*static*/ MediaProfiles::VideoEncoderCap*
684MediaProfiles::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*
691MediaProfiles::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
699MediaProfiles::createDefaultVideoEncoders(MediaProfiles *profiles)
700{
701 profiles->mVideoEncoders.add(createDefaultH263VideoEncoderCap());
702 profiles->mVideoEncoders.add(createDefaultM4vVideoEncoderCap());
703}
704
705/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700706MediaProfiles::createDefaultCamcorderTimeLapseQcifProfile(camcorder_quality quality)
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700707{
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;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700715 profile->mQuality = quality;
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700716 profile->mDuration = 60;
717 profile->mVideoCodec = videoCodec;
718 profile->mAudioCodec = audioCodec;
719 return profile;
720}
721
722/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700723MediaProfiles::createDefaultCamcorderTimeLapse480pProfile(camcorder_quality quality)
James Dong1d7491b2010-01-19 17:45:38 -0800724{
725 MediaProfiles::VideoCodec *videoCodec =
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700726 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 20000000, 720, 480, 20);
James Dong1d7491b2010-01-19 17:45:38 -0800727
728 AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800729 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
730 profile->mCameraId = 0;
James Dong1d7491b2010-01-19 17:45:38 -0800731 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700732 profile->mQuality = quality;
James Dong1d7491b2010-01-19 17:45:38 -0800733 profile->mDuration = 60;
734 profile->mVideoCodec = videoCodec;
735 profile->mAudioCodec = audioCodec;
736 return profile;
737}
738
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700739/*static*/ void
740MediaProfiles::createDefaultCamcorderTimeLapseLowProfiles(
741 MediaProfiles::CamcorderProfile **lowTimeLapseProfile,
742 MediaProfiles::CamcorderProfile **lowSpecificTimeLapseProfile) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800743 *lowTimeLapseProfile = createDefaultCamcorderTimeLapseQcifProfile(
744 CAMCORDER_QUALITY_TIME_LAPSE_LOW);
745 *lowSpecificTimeLapseProfile = createDefaultCamcorderTimeLapseQcifProfile(
746 CAMCORDER_QUALITY_TIME_LAPSE_QCIF);
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700747}
748
749/*static*/ void
750MediaProfiles::createDefaultCamcorderTimeLapseHighProfiles(
751 MediaProfiles::CamcorderProfile **highTimeLapseProfile,
752 MediaProfiles::CamcorderProfile **highSpecificTimeLapseProfile) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800753 *highTimeLapseProfile = createDefaultCamcorderTimeLapse480pProfile(
754 CAMCORDER_QUALITY_TIME_LAPSE_HIGH);
755 *highSpecificTimeLapseProfile = createDefaultCamcorderTimeLapse480pProfile(
756 CAMCORDER_QUALITY_TIME_LAPSE_480P);
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700757}
758
James Dong1d7491b2010-01-19 17:45:38 -0800759/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700760MediaProfiles::createDefaultCamcorderQcifProfile(camcorder_quality quality)
James Dong1d7491b2010-01-19 17:45:38 -0800761{
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;
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800769 profile->mCameraId = 0;
James Dong1d7491b2010-01-19 17:45:38 -0800770 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700771 profile->mQuality = quality;
James Dong1d7491b2010-01-19 17:45:38 -0800772 profile->mDuration = 30;
773 profile->mVideoCodec = videoCodec;
774 profile->mAudioCodec = audioCodec;
775 return profile;
776}
777
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700778/*static*/ MediaProfiles::CamcorderProfile*
779MediaProfiles::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
796MediaProfiles::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
804MediaProfiles::createDefaultCamcorderHighProfiles(
805 MediaProfiles::CamcorderProfile **highProfile,
806 MediaProfiles::CamcorderProfile **highSpecificProfile) {
807 *highProfile = createDefaultCamcorderCifProfile(CAMCORDER_QUALITY_HIGH);
808 *highSpecificProfile = createDefaultCamcorderCifProfile(CAMCORDER_QUALITY_CIF);
809}
810
James Dong1d7491b2010-01-19 17:45:38 -0800811/*static*/ void
812MediaProfiles::createDefaultCamcorderProfiles(MediaProfiles *profiles)
813{
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700814 // 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;
Glenn Kastenb187de12014-12-30 08:18:15 -0800834 createDefaultCamcorderTimeLapseHighProfiles(&highTimeLapseProfile,
835 &highSpecificTimeLapseProfile);
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700836 profiles->mCamcorderProfiles.add(highTimeLapseProfile);
837 profiles->mCamcorderProfiles.add(highSpecificTimeLapseProfile);
James Dong8031ec72011-03-16 14:09:50 -0700838
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);
James Dong1d7491b2010-01-19 17:45:38 -0800843}
844
845/*static*/ void
846MediaProfiles::createDefaultAudioEncoders(MediaProfiles *profiles)
847{
848 profiles->mAudioEncoders.add(createDefaultAmrNBEncoderCap());
849}
850
851/*static*/ void
852MediaProfiles::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
861MediaProfiles::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
870MediaProfiles::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*
877MediaProfiles::createDefaultAmrNBEncoderCap()
878{
879 return new MediaProfiles::AudioEncoderCap(
880 AUDIO_ENCODER_AMR_NB, 5525, 12200, 8000, 8000, 1, 1);
881}
882
James Dongf5a83852010-02-23 17:21:44 -0800883/*static*/ void
884MediaProfiles::createDefaultImageEncodingQualityLevels(MediaProfiles *profiles)
885{
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800886 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);
James Dongf5a83852010-02-23 17:21:44 -0800892}
893
James Dong1d7491b2010-01-19 17:45:38 -0800894/*static*/ MediaProfiles*
895MediaProfiles::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);
James Dongf5a83852010-02-23 17:21:44 -0800904 createDefaultImageEncodingQualityLevels(profiles);
James Dong1d7491b2010-01-19 17:45:38 -0800905 return profiles;
906}
907
Pawin Vongmasad7db05b2017-05-03 04:19:20 -0700908bool 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
James Dong1d7491b2010-01-19 17:45:38 -0800914/*static*/ MediaProfiles*
915MediaProfiles::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)) {
Steve Block29357bc2012-01-06 19:20:56 +0000932 ALOGE("failed to enable DTD support in the xml file");
James Dong1d7491b2010-01-19 17:45:38 -0800933 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) {
Steve Block29357bc2012-01-06 19:20:56 +0000942 ALOGE("failed to in call to XML_GetBuffer()");
James Dong1d7491b2010-01-19 17:45:38 -0800943 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) {
Steve Block29357bc2012-01-06 19:20:56 +0000950 ALOGE("failed in call to read");
James Dong1d7491b2010-01-19 17:45:38 -0800951 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
961exit:
962 ::XML_ParserFree(parser);
963 ::fclose(fp);
James Dong1d7491b2010-01-19 17:45:38 -0800964 return profiles;
965}
966
967Vector<output_format> MediaProfiles::getOutputFileFormats() const
968{
969 return mEncoderOutputFileFormats; // copy out
970}
971
972Vector<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
981int MediaProfiles::getVideoEncoderParamByName(const char *name, video_encoder codec) const
982{
Steve Block3856b092011-10-20 11:56:00 +0100983 ALOGV("getVideoEncoderParamByName: %s for codec %d", name, codec);
James Dong1d7491b2010-01-19 17:45:38 -0800984 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) {
Steve Block29357bc2012-01-06 19:20:56 +0000992 ALOGE("The given video encoder %d is not found", codec);
James Dong1d7491b2010-01-19 17:45:38 -0800993 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
Steve Block29357bc2012-01-06 19:20:56 +00001005 ALOGE("The given video encoder param name %s is not found", name);
James Dong1d7491b2010-01-19 17:45:38 -08001006 return -1;
1007}
Hong Tengcabd5f82011-07-06 18:33:09 -07001008
James Dong1d7491b2010-01-19 17:45:38 -08001009Vector<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
1018int MediaProfiles::getAudioEncoderParamByName(const char *name, audio_encoder codec) const
1019{
Steve Block3856b092011-10-20 11:56:00 +01001020 ALOGV("getAudioEncoderParamByName: %s for codec %d", name, codec);
James Dong1d7491b2010-01-19 17:45:38 -08001021 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) {
Steve Block29357bc2012-01-06 19:20:56 +00001029 ALOGE("The given audio encoder %d is not found", codec);
James Dong1d7491b2010-01-19 17:45:38 -08001030 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
Steve Block29357bc2012-01-06 19:20:56 +00001040 ALOGE("The given audio encoder param name %s is not found", name);
James Dong1d7491b2010-01-19 17:45:38 -08001041 return -1;
1042}
1043
1044Vector<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
1053Vector<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
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001062int MediaProfiles::getCamcorderProfileIndex(int cameraId, camcorder_quality quality) const
James Dong1d7491b2010-01-19 17:45:38 -08001063{
James Dong1d7491b2010-01-19 17:45:38 -08001064 int index = -1;
1065 for (size_t i = 0, n = mCamcorderProfiles.size(); i < n; ++i) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001066 if (mCamcorderProfiles[i]->mCameraId == cameraId &&
1067 mCamcorderProfiles[i]->mQuality == quality) {
James Dong1d7491b2010-01-19 17:45:38 -08001068 index = i;
1069 break;
1070 }
1071 }
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001072 return index;
1073}
1074
1075int MediaProfiles::getCamcorderProfileParamByName(const char *name,
1076 int cameraId,
1077 camcorder_quality quality) const
1078{
Steve Block3856b092011-10-20 11:56:00 +01001079 ALOGV("getCamcorderProfileParamByName: %s for camera %d, quality %d",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001080 name, cameraId, quality);
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001081
1082 int index = getCamcorderProfileIndex(cameraId, quality);
James Dong1d7491b2010-01-19 17:45:38 -08001083 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +00001084 ALOGE("The given camcorder profile camera %d quality %d is not found",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001085 cameraId, quality);
James Dong1d7491b2010-01-19 17:45:38 -08001086 return -1;
1087 }
1088
James Dongf5a83852010-02-23 17:21:44 -08001089 if (!strcmp("duration", name)) return mCamcorderProfiles[index]->mDuration;
James Dong1d7491b2010-01-19 17:45:38 -08001090 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
Steve Block29357bc2012-01-06 19:20:56 +00001101 ALOGE("The given camcorder profile param id %d name %s is not found", cameraId, name);
James Dong1d7491b2010-01-19 17:45:38 -08001102 return -1;
1103}
1104
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001105bool MediaProfiles::hasCamcorderProfile(int cameraId, camcorder_quality quality) const
1106{
1107 return (getCamcorderProfileIndex(cameraId, quality) != -1);
1108}
1109
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001110Vector<int> MediaProfiles::getImageEncodingQualityLevels(int cameraId) const
James Dongf5a83852010-02-23 17:21:44 -08001111{
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001112 Vector<int> result;
1113 ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
1114 if (levels != NULL) {
1115 result = levels->mLevels; // copy out
1116 }
1117 return result;
James Dongf5a83852010-02-23 17:21:44 -08001118}
1119
James Dong0f056292011-05-09 18:49:31 -07001120int 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 }
Steve Block3856b092011-10-20 11:56:00 +01001126 ALOGV("offsetTime=%d ms and cameraId=%d", offsetTimeMs, cameraId);
James Dong0f056292011-05-09 18:49:31 -07001127 return offsetTimeMs;
1128}
1129
James Dong1d7491b2010-01-19 17:45:38 -08001130MediaProfiles::~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