blob: e8839baad1931c859985b02031b8a57e4d644dc5 [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]);
Alex Zhang032f7a12020-04-17 16:08:24 -0700243 if (codec == -1) {
244 ALOGE("MediaProfiles::createVideoCodec failed to locate codec %s", atts[1]);
245 return nullptr;
246 }
James Dong1d7491b2010-01-19 17:45:38 -0800247
248 MediaProfiles::VideoCodec *videoCodec =
249 new MediaProfiles::VideoCodec(static_cast<video_encoder>(codec),
250 atoi(atts[3]), atoi(atts[5]), atoi(atts[7]), atoi(atts[9]));
251 logVideoCodec(*videoCodec);
252
253 size_t nCamcorderProfiles;
254 CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
255 profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mVideoCodec = videoCodec;
256 return videoCodec;
257}
258
259/*static*/ MediaProfiles::AudioCodec*
260MediaProfiles::createAudioCodec(const char **atts, MediaProfiles *profiles)
261{
262 CHECK(!strcmp("codec", atts[0]) &&
263 !strcmp("bitRate", atts[2]) &&
264 !strcmp("sampleRate", atts[4]) &&
265 !strcmp("channels", atts[6]));
266 const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
267 const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
Alex Zhang032f7a12020-04-17 16:08:24 -0700268 if (codec == -1) {
269 ALOGE("MediaProfiles::createAudioCodec failed to locate codec %s", atts[1]);
270 return nullptr;
271 }
James Dong1d7491b2010-01-19 17:45:38 -0800272
273 MediaProfiles::AudioCodec *audioCodec =
274 new MediaProfiles::AudioCodec(static_cast<audio_encoder>(codec),
275 atoi(atts[3]), atoi(atts[5]), atoi(atts[7]));
276 logAudioCodec(*audioCodec);
277
278 size_t nCamcorderProfiles;
279 CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
280 profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mAudioCodec = audioCodec;
281 return audioCodec;
282}
283/*static*/ MediaProfiles::AudioDecoderCap*
284MediaProfiles::createAudioDecoderCap(const char **atts)
285{
286 CHECK(!strcmp("name", atts[0]) &&
287 !strcmp("enabled", atts[2]));
288
289 const size_t nMappings = sizeof(sAudioDecoderNameMap)/sizeof(sAudioDecoderNameMap[0]);
290 const int codec = findTagForName(sAudioDecoderNameMap, nMappings, atts[1]);
Alex Zhang032f7a12020-04-17 16:08:24 -0700291 if (codec == -1) {
292 ALOGE("MediaProfiles::createAudioDecoderCap failed to locate codec %s", atts[1]);
293 return nullptr;
294 }
James Dong1d7491b2010-01-19 17:45:38 -0800295
296 MediaProfiles::AudioDecoderCap *cap =
297 new MediaProfiles::AudioDecoderCap(static_cast<audio_decoder>(codec));
298 logAudioDecoderCap(*cap);
299 return cap;
300}
301
302/*static*/ MediaProfiles::VideoDecoderCap*
303MediaProfiles::createVideoDecoderCap(const char **atts)
304{
305 CHECK(!strcmp("name", atts[0]) &&
306 !strcmp("enabled", atts[2]));
307
308 const size_t nMappings = sizeof(sVideoDecoderNameMap)/sizeof(sVideoDecoderNameMap[0]);
309 const int codec = findTagForName(sVideoDecoderNameMap, nMappings, atts[1]);
Alex Zhang032f7a12020-04-17 16:08:24 -0700310 if (codec == -1) {
311 ALOGE("MediaProfiles::createVideoDecoderCap failed to locate codec %s", atts[1]);
312 return nullptr;
313 }
James Dong1d7491b2010-01-19 17:45:38 -0800314
315 MediaProfiles::VideoDecoderCap *cap =
316 new MediaProfiles::VideoDecoderCap(static_cast<video_decoder>(codec));
317 logVideoDecoderCap(*cap);
318 return cap;
319}
320
321/*static*/ MediaProfiles::VideoEncoderCap*
322MediaProfiles::createVideoEncoderCap(const char **atts)
323{
324 CHECK(!strcmp("name", atts[0]) &&
325 !strcmp("enabled", atts[2]) &&
326 !strcmp("minBitRate", atts[4]) &&
327 !strcmp("maxBitRate", atts[6]) &&
328 !strcmp("minFrameWidth", atts[8]) &&
329 !strcmp("maxFrameWidth", atts[10]) &&
330 !strcmp("minFrameHeight", atts[12]) &&
331 !strcmp("maxFrameHeight", atts[14]) &&
332 !strcmp("minFrameRate", atts[16]) &&
333 !strcmp("maxFrameRate", atts[18]));
334
335 const size_t nMappings = sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
336 const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
Alex Zhang032f7a12020-04-17 16:08:24 -0700337 if (codec == -1) {
338 ALOGE("MediaProfiles::createVideoEncoderCap failed to locate codec %s", atts[1]);
339 return nullptr;
340 }
James Dong1d7491b2010-01-19 17:45:38 -0800341
342 MediaProfiles::VideoEncoderCap *cap =
343 new MediaProfiles::VideoEncoderCap(static_cast<video_encoder>(codec),
344 atoi(atts[5]), atoi(atts[7]), atoi(atts[9]), atoi(atts[11]), atoi(atts[13]),
345 atoi(atts[15]), atoi(atts[17]), atoi(atts[19]));
346 logVideoEncoderCap(*cap);
347 return cap;
348}
349
350/*static*/ MediaProfiles::AudioEncoderCap*
351MediaProfiles::createAudioEncoderCap(const char **atts)
352{
353 CHECK(!strcmp("name", atts[0]) &&
354 !strcmp("enabled", atts[2]) &&
355 !strcmp("minBitRate", atts[4]) &&
356 !strcmp("maxBitRate", atts[6]) &&
357 !strcmp("minSampleRate", atts[8]) &&
358 !strcmp("maxSampleRate", atts[10]) &&
359 !strcmp("minChannels", atts[12]) &&
360 !strcmp("maxChannels", atts[14]));
361
362 const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
363 const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
Alex Zhang032f7a12020-04-17 16:08:24 -0700364 if (codec == -1) {
365 ALOGE("MediaProfiles::createAudioEncoderCap failed to locate codec %s", atts[1]);
366 return nullptr;
367 }
James Dong1d7491b2010-01-19 17:45:38 -0800368
369 MediaProfiles::AudioEncoderCap *cap =
Glenn Kastenb187de12014-12-30 08:18:15 -0800370 new MediaProfiles::AudioEncoderCap(static_cast<audio_encoder>(codec), atoi(atts[5]),
371 atoi(atts[7]), atoi(atts[9]), atoi(atts[11]), atoi(atts[13]), atoi(atts[15]));
James Dong1d7491b2010-01-19 17:45:38 -0800372 logAudioEncoderCap(*cap);
373 return cap;
374}
375
376/*static*/ output_format
377MediaProfiles::createEncoderOutputFileFormat(const char **atts)
378{
379 CHECK(!strcmp("name", atts[0]));
380
381 const size_t nMappings =sizeof(sFileFormatMap)/sizeof(sFileFormatMap[0]);
382 const int format = findTagForName(sFileFormatMap, nMappings, atts[1]);
383 CHECK(format != -1);
384
385 return static_cast<output_format>(format);
386}
387
James Dong2a7e0a12011-02-28 21:07:39 -0800388static bool isCameraIdFound(int cameraId, const Vector<int>& cameraIds) {
389 for (int i = 0, n = cameraIds.size(); i < n; ++i) {
390 if (cameraId == cameraIds[i]) {
391 return true;
392 }
393 }
394 return false;
395}
396
James Dong1d7491b2010-01-19 17:45:38 -0800397/*static*/ MediaProfiles::CamcorderProfile*
James Dong2a7e0a12011-02-28 21:07:39 -0800398MediaProfiles::createCamcorderProfile(int cameraId, const char **atts, Vector<int>& cameraIds)
James Dong1d7491b2010-01-19 17:45:38 -0800399{
400 CHECK(!strcmp("quality", atts[0]) &&
401 !strcmp("fileFormat", atts[2]) &&
402 !strcmp("duration", atts[4]));
403
Glenn Kastenb187de12014-12-30 08:18:15 -0800404 const size_t nProfileMappings = sizeof(sCamcorderQualityNameMap)/
405 sizeof(sCamcorderQualityNameMap[0]);
James Dong1d7491b2010-01-19 17:45:38 -0800406 const int quality = findTagForName(sCamcorderQualityNameMap, nProfileMappings, atts[1]);
Alex Zhang032f7a12020-04-17 16:08:24 -0700407 if (quality == -1) {
408 ALOGE("MediaProfiles::createCamcorderProfile failed to locate quality %s", atts[1]);
409 return nullptr;
410 }
James Dong1d7491b2010-01-19 17:45:38 -0800411
412 const size_t nFormatMappings = sizeof(sFileFormatMap)/sizeof(sFileFormatMap[0]);
413 const int fileFormat = findTagForName(sFileFormatMap, nFormatMappings, atts[3]);
Alex Zhang032f7a12020-04-17 16:08:24 -0700414 if (fileFormat == -1) {
415 ALOGE("MediaProfiles::createCamcorderProfile failed to locate file format %s", atts[1]);
416 return nullptr;
417 }
James Dong1d7491b2010-01-19 17:45:38 -0800418
419 MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800420 profile->mCameraId = cameraId;
James Dong2a7e0a12011-02-28 21:07:39 -0800421 if (!isCameraIdFound(cameraId, cameraIds)) {
422 cameraIds.add(cameraId);
423 }
James Dong1d7491b2010-01-19 17:45:38 -0800424 profile->mFileFormat = static_cast<output_format>(fileFormat);
425 profile->mQuality = static_cast<camcorder_quality>(quality);
426 profile->mDuration = atoi(atts[5]);
427 return profile;
428}
429
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800430MediaProfiles::ImageEncodingQualityLevels*
431MediaProfiles::findImageEncodingQualityLevels(int cameraId) const
432{
433 int n = mImageEncodingQualityLevels.size();
434 for (int i = 0; i < n; i++) {
435 ImageEncodingQualityLevels *levels = mImageEncodingQualityLevels[i];
436 if (levels->mCameraId == cameraId) {
437 return levels;
438 }
439 }
440 return NULL;
441}
442
443void MediaProfiles::addImageEncodingQualityLevel(int cameraId, const char** atts)
James Dongf5a83852010-02-23 17:21:44 -0800444{
445 CHECK(!strcmp("quality", atts[0]));
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800446 int quality = atoi(atts[1]);
Glenn Kasten90bebef2012-01-27 15:24:38 -0800447 ALOGV("%s: cameraId=%d, quality=%d", __func__, cameraId, quality);
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800448 ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
449
450 if (levels == NULL) {
451 levels = new ImageEncodingQualityLevels();
452 levels->mCameraId = cameraId;
453 mImageEncodingQualityLevels.add(levels);
454 }
455
456 levels->mLevels.add(quality);
457}
458
459/*static*/ int
460MediaProfiles::getCameraId(const char** atts)
461{
462 if (!atts[0]) return 0; // default cameraId = 0
463 CHECK(!strcmp("cameraId", atts[0]));
James Dongf5a83852010-02-23 17:21:44 -0800464 return atoi(atts[1]);
465}
466
James Dong0f056292011-05-09 18:49:31 -0700467void MediaProfiles::addStartTimeOffset(int cameraId, const char** atts)
468{
Eric Laurentb1eb1a02012-10-22 17:44:24 -0700469 int offsetTimeMs = 1000;
James Dong0f056292011-05-09 18:49:31 -0700470 if (atts[2]) {
471 CHECK(!strcmp("startOffsetMs", atts[2]));
472 offsetTimeMs = atoi(atts[3]);
473 }
474
Steve Block3856b092011-10-20 11:56:00 +0100475 ALOGV("%s: cameraId=%d, offset=%d ms", __func__, cameraId, offsetTimeMs);
James Dong0f056292011-05-09 18:49:31 -0700476 mStartTimeOffsets.replaceValueFor(cameraId, offsetTimeMs);
477}
Hong Tengcabd5f82011-07-06 18:33:09 -0700478
James Dong1d7491b2010-01-19 17:45:38 -0800479/*static*/ void
480MediaProfiles::startElementHandler(void *userData, const char *name, const char **atts)
481{
482 MediaProfiles *profiles = (MediaProfiles *) userData;
483 if (strcmp("Video", name) == 0) {
484 createVideoCodec(atts, profiles);
485 } else if (strcmp("Audio", name) == 0) {
486 createAudioCodec(atts, profiles);
487 } else if (strcmp("VideoEncoderCap", name) == 0 &&
488 strcmp("true", atts[3]) == 0) {
Alex Zhang032f7a12020-04-17 16:08:24 -0700489 MediaProfiles::VideoEncoderCap* cap = createVideoEncoderCap(atts);
490 if (cap != nullptr) {
491 profiles->mVideoEncoders.add(cap);
492 }
James Dong1d7491b2010-01-19 17:45:38 -0800493 } else if (strcmp("AudioEncoderCap", name) == 0 &&
494 strcmp("true", atts[3]) == 0) {
Alex Zhang032f7a12020-04-17 16:08:24 -0700495 MediaProfiles::AudioEncoderCap* cap = createAudioEncoderCap(atts);
496 if (cap != nullptr) {
497 profiles->mAudioEncoders.add(cap);
498 }
James Dong1d7491b2010-01-19 17:45:38 -0800499 } else if (strcmp("VideoDecoderCap", name) == 0 &&
500 strcmp("true", atts[3]) == 0) {
Alex Zhang032f7a12020-04-17 16:08:24 -0700501 MediaProfiles::VideoDecoderCap* cap = createVideoDecoderCap(atts);
502 if (cap != nullptr) {
503 profiles->mVideoDecoders.add(cap);
504 }
James Dong1d7491b2010-01-19 17:45:38 -0800505 } else if (strcmp("AudioDecoderCap", name) == 0 &&
506 strcmp("true", atts[3]) == 0) {
Alex Zhang032f7a12020-04-17 16:08:24 -0700507 MediaProfiles::AudioDecoderCap* cap = createAudioDecoderCap(atts);
508 if (cap != nullptr) {
509 profiles->mAudioDecoders.add(cap);
510 }
James Dong1d7491b2010-01-19 17:45:38 -0800511 } else if (strcmp("EncoderOutputFileFormat", name) == 0) {
512 profiles->mEncoderOutputFileFormats.add(createEncoderOutputFileFormat(atts));
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800513 } else if (strcmp("CamcorderProfiles", name) == 0) {
514 profiles->mCurrentCameraId = getCameraId(atts);
James Dong0f056292011-05-09 18:49:31 -0700515 profiles->addStartTimeOffset(profiles->mCurrentCameraId, atts);
James Dong1d7491b2010-01-19 17:45:38 -0800516 } else if (strcmp("EncoderProfile", name) == 0) {
Alex Zhang032f7a12020-04-17 16:08:24 -0700517 MediaProfiles::CamcorderProfile* profile = createCamcorderProfile(
518 profiles->mCurrentCameraId, atts, profiles->mCameraIds);
519 if (profile != nullptr) {
520 profiles->mCamcorderProfiles.add(profile);
521 }
James Dongf5a83852010-02-23 17:21:44 -0800522 } else if (strcmp("ImageEncoding", name) == 0) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800523 profiles->addImageEncodingQualityLevel(profiles->mCurrentCameraId, atts);
James Dong1d7491b2010-01-19 17:45:38 -0800524 }
525}
526
James Dong2a7e0a12011-02-28 21:07:39 -0800527static bool isCamcorderProfile(camcorder_quality quality) {
528 return quality >= CAMCORDER_QUALITY_LIST_START &&
529 quality <= CAMCORDER_QUALITY_LIST_END;
530}
531
532static bool isTimelapseProfile(camcorder_quality quality) {
533 return quality >= CAMCORDER_QUALITY_TIME_LAPSE_LIST_START &&
534 quality <= CAMCORDER_QUALITY_TIME_LAPSE_LIST_END;
535}
536
Zhijun Hee0790972014-07-23 15:17:26 -0700537static bool isHighSpeedProfile(camcorder_quality quality) {
538 return quality >= CAMCORDER_QUALITY_HIGH_SPEED_LIST_START &&
539 quality <= CAMCORDER_QUALITY_HIGH_SPEED_LIST_END;
540}
541
James Dong2a7e0a12011-02-28 21:07:39 -0800542void MediaProfiles::initRequiredProfileRefs(const Vector<int>& cameraIds) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700543 ALOGV("Number of camera ids: %zu", cameraIds.size());
James Dong2a7e0a12011-02-28 21:07:39 -0800544 CHECK(cameraIds.size() > 0);
545 mRequiredProfileRefs = new RequiredProfiles[cameraIds.size()];
546 for (size_t i = 0, n = cameraIds.size(); i < n; ++i) {
547 mRequiredProfileRefs[i].mCameraId = cameraIds[i];
548 for (size_t j = 0; j < kNumRequiredProfiles; ++j) {
549 mRequiredProfileRefs[i].mRefs[j].mHasRefProfile = false;
550 mRequiredProfileRefs[i].mRefs[j].mRefProfileIndex = -1;
551 if ((j & 1) == 0) { // low resolution
552 mRequiredProfileRefs[i].mRefs[j].mResolutionProduct = 0x7FFFFFFF;
553 } else { // high resolution
554 mRequiredProfileRefs[i].mRefs[j].mResolutionProduct = 0;
555 }
556 }
557 }
558}
559
560int MediaProfiles::getRequiredProfileRefIndex(int cameraId) {
561 for (size_t i = 0, n = mCameraIds.size(); i < n; ++i) {
562 if (mCameraIds[i] == cameraId) {
563 return i;
564 }
565 }
566 return -1;
567}
568
569void MediaProfiles::checkAndAddRequiredProfilesIfNecessary() {
570 if (sIsInitialized) {
571 return;
572 }
573
574 initRequiredProfileRefs(mCameraIds);
575
576 for (size_t i = 0, n = mCamcorderProfiles.size(); i < n; ++i) {
577 int product = mCamcorderProfiles[i]->mVideoCodec->mFrameWidth *
578 mCamcorderProfiles[i]->mVideoCodec->mFrameHeight;
579
580 camcorder_quality quality = mCamcorderProfiles[i]->mQuality;
581 int cameraId = mCamcorderProfiles[i]->mCameraId;
582 int index = -1;
583 int refIndex = getRequiredProfileRefIndex(cameraId);
584 CHECK(refIndex != -1);
585 RequiredProfileRefInfo *info;
586 camcorder_quality refQuality;
James Dong2a7e0a12011-02-28 21:07:39 -0800587
Zhijun Hee0790972014-07-23 15:17:26 -0700588 // Check high and low from either camcorder profile, timelapse profile
589 // or high speed profile, but not all of them. Default, check camcorder profile
James Dong2a7e0a12011-02-28 21:07:39 -0800590 size_t j = 0;
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200591 size_t o = 2;
James Dong2a7e0a12011-02-28 21:07:39 -0800592 if (isTimelapseProfile(quality)) {
593 // Check timelapse profile instead.
594 j = 2;
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200595 o = kNumRequiredProfiles;
Zhijun Hee0790972014-07-23 15:17:26 -0700596 } else if (isHighSpeedProfile(quality)) {
597 // Skip the check for high speed profile.
598 continue;
James Dong2a7e0a12011-02-28 21:07:39 -0800599 } else {
600 // Must be camcorder profile.
601 CHECK(isCamcorderProfile(quality));
602 }
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200603 for (; j < o; ++j) {
James Dong2a7e0a12011-02-28 21:07:39 -0800604 info = &(mRequiredProfileRefs[refIndex].mRefs[j]);
605 if ((j % 2 == 0 && product > info->mResolutionProduct) || // low
606 (j % 2 != 0 && product < info->mResolutionProduct)) { // high
607 continue;
608 }
609 switch (j) {
610 case 0:
611 refQuality = CAMCORDER_QUALITY_LOW;
612 break;
613 case 1:
614 refQuality = CAMCORDER_QUALITY_HIGH;
615 break;
616 case 2:
617 refQuality = CAMCORDER_QUALITY_TIME_LAPSE_LOW;
618 break;
619 case 3:
620 refQuality = CAMCORDER_QUALITY_TIME_LAPSE_HIGH;
621 break;
622 default:
623 CHECK(!"Should never reach here");
624 }
625
626 if (!info->mHasRefProfile) {
627 index = getCamcorderProfileIndex(cameraId, refQuality);
628 }
629 if (index == -1) {
630 // New high or low quality profile is found.
631 // Update its reference.
632 info->mHasRefProfile = true;
633 info->mRefProfileIndex = i;
634 info->mResolutionProduct = product;
635 }
636 }
637 }
638
639 for (size_t cameraId = 0; cameraId < mCameraIds.size(); ++cameraId) {
640 for (size_t j = 0; j < kNumRequiredProfiles; ++j) {
641 int refIndex = getRequiredProfileRefIndex(cameraId);
642 CHECK(refIndex != -1);
643 RequiredProfileRefInfo *info =
644 &mRequiredProfileRefs[refIndex].mRefs[j];
645
646 if (info->mHasRefProfile) {
647
George Burgess IV215545b2018-07-25 10:06:30 -0700648 std::unique_ptr<CamcorderProfile> profile =
649 std::make_unique<CamcorderProfile>(
James Dong2a7e0a12011-02-28 21:07:39 -0800650 *mCamcorderProfiles[info->mRefProfileIndex]);
651
652 // Overwrite the quality
653 switch (j % kNumRequiredProfiles) {
654 case 0:
655 profile->mQuality = CAMCORDER_QUALITY_LOW;
656 break;
657 case 1:
658 profile->mQuality = CAMCORDER_QUALITY_HIGH;
659 break;
660 case 2:
661 profile->mQuality = CAMCORDER_QUALITY_TIME_LAPSE_LOW;
662 break;
663 case 3:
664 profile->mQuality = CAMCORDER_QUALITY_TIME_LAPSE_HIGH;
665 break;
666 default:
667 CHECK(!"Should never come here");
668 }
669
670 int index = getCamcorderProfileIndex(cameraId, profile->mQuality);
671 if (index != -1) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700672 ALOGV("Profile quality %d for camera %zu already exists",
James Dong2a7e0a12011-02-28 21:07:39 -0800673 profile->mQuality, cameraId);
674 CHECK(index == refIndex);
675 continue;
676 }
677
678 // Insert the new profile
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700679 ALOGV("Add a profile: quality %d=>%d for camera %zu",
James Dong2a7e0a12011-02-28 21:07:39 -0800680 mCamcorderProfiles[info->mRefProfileIndex]->mQuality,
681 profile->mQuality, cameraId);
682
George Burgess IV215545b2018-07-25 10:06:30 -0700683 mCamcorderProfiles.add(profile.release());
James Dong2a7e0a12011-02-28 21:07:39 -0800684 }
685 }
686 }
687}
688
James Dong1d7491b2010-01-19 17:45:38 -0800689/*static*/ MediaProfiles*
690MediaProfiles::getInstance()
691{
Steve Block3856b092011-10-20 11:56:00 +0100692 ALOGV("getInstance");
James Dong1d7491b2010-01-19 17:45:38 -0800693 Mutex::Autolock lock(sLock);
694 if (!sIsInitialized) {
695 char value[PROPERTY_VALUE_MAX];
696 if (property_get("media.settings.xml", value, NULL) <= 0) {
Pawin Vongmasad7db05b2017-05-03 04:19:20 -0700697 const char* xmlFile = nullptr;
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -0800698 for (auto const& f : getXmlPaths()) {
Pawin Vongmasad7db05b2017-05-03 04:19:20 -0700699 if (checkXmlFile(f)) {
700 xmlFile = f;
701 break;
702 }
703 }
704 if (xmlFile == nullptr) {
705 ALOGW("Could not find a validated xml file. "
706 "Using the default instance instead.");
James Dong1d7491b2010-01-19 17:45:38 -0800707 sInstance = createDefaultInstance();
708 } else {
Pawin Vongmasad7db05b2017-05-03 04:19:20 -0700709 sInstance = createInstanceFromXmlFile(xmlFile);
James Dong1d7491b2010-01-19 17:45:38 -0800710 }
711 } else {
712 sInstance = createInstanceFromXmlFile(value);
713 }
James Dong2a7e0a12011-02-28 21:07:39 -0800714 CHECK(sInstance != NULL);
715 sInstance->checkAndAddRequiredProfilesIfNecessary();
716 sIsInitialized = true;
James Dong1d7491b2010-01-19 17:45:38 -0800717 }
718
719 return sInstance;
720}
721
722/*static*/ MediaProfiles::VideoEncoderCap*
723MediaProfiles::createDefaultH263VideoEncoderCap()
724{
725 return new MediaProfiles::VideoEncoderCap(
726 VIDEO_ENCODER_H263, 192000, 420000, 176, 352, 144, 288, 1, 20);
727}
728
729/*static*/ MediaProfiles::VideoEncoderCap*
730MediaProfiles::createDefaultM4vVideoEncoderCap()
731{
732 return new MediaProfiles::VideoEncoderCap(
733 VIDEO_ENCODER_MPEG_4_SP, 192000, 420000, 176, 352, 144, 288, 1, 20);
734}
735
736
737/*static*/ void
738MediaProfiles::createDefaultVideoEncoders(MediaProfiles *profiles)
739{
740 profiles->mVideoEncoders.add(createDefaultH263VideoEncoderCap());
741 profiles->mVideoEncoders.add(createDefaultM4vVideoEncoderCap());
742}
743
744/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700745MediaProfiles::createDefaultCamcorderTimeLapseQcifProfile(camcorder_quality quality)
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700746{
747 MediaProfiles::VideoCodec *videoCodec =
748 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 1000000, 176, 144, 20);
749
750 AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
751 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
752 profile->mCameraId = 0;
753 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700754 profile->mQuality = quality;
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700755 profile->mDuration = 60;
756 profile->mVideoCodec = videoCodec;
757 profile->mAudioCodec = audioCodec;
758 return profile;
759}
760
761/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700762MediaProfiles::createDefaultCamcorderTimeLapse480pProfile(camcorder_quality quality)
James Dong1d7491b2010-01-19 17:45:38 -0800763{
764 MediaProfiles::VideoCodec *videoCodec =
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700765 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 20000000, 720, 480, 20);
James Dong1d7491b2010-01-19 17:45:38 -0800766
767 AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800768 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
769 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 = 60;
773 profile->mVideoCodec = videoCodec;
774 profile->mAudioCodec = audioCodec;
775 return profile;
776}
777
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700778/*static*/ void
779MediaProfiles::createDefaultCamcorderTimeLapseLowProfiles(
780 MediaProfiles::CamcorderProfile **lowTimeLapseProfile,
781 MediaProfiles::CamcorderProfile **lowSpecificTimeLapseProfile) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800782 *lowTimeLapseProfile = createDefaultCamcorderTimeLapseQcifProfile(
783 CAMCORDER_QUALITY_TIME_LAPSE_LOW);
784 *lowSpecificTimeLapseProfile = createDefaultCamcorderTimeLapseQcifProfile(
785 CAMCORDER_QUALITY_TIME_LAPSE_QCIF);
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700786}
787
788/*static*/ void
789MediaProfiles::createDefaultCamcorderTimeLapseHighProfiles(
790 MediaProfiles::CamcorderProfile **highTimeLapseProfile,
791 MediaProfiles::CamcorderProfile **highSpecificTimeLapseProfile) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800792 *highTimeLapseProfile = createDefaultCamcorderTimeLapse480pProfile(
793 CAMCORDER_QUALITY_TIME_LAPSE_HIGH);
794 *highSpecificTimeLapseProfile = createDefaultCamcorderTimeLapse480pProfile(
795 CAMCORDER_QUALITY_TIME_LAPSE_480P);
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700796}
797
James Dong1d7491b2010-01-19 17:45:38 -0800798/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700799MediaProfiles::createDefaultCamcorderQcifProfile(camcorder_quality quality)
James Dong1d7491b2010-01-19 17:45:38 -0800800{
801 MediaProfiles::VideoCodec *videoCodec =
802 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 192000, 176, 144, 20);
803
804 MediaProfiles::AudioCodec *audioCodec =
805 new MediaProfiles::AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
806
807 MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800808 profile->mCameraId = 0;
James Dong1d7491b2010-01-19 17:45:38 -0800809 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700810 profile->mQuality = quality;
James Dong1d7491b2010-01-19 17:45:38 -0800811 profile->mDuration = 30;
812 profile->mVideoCodec = videoCodec;
813 profile->mAudioCodec = audioCodec;
814 return profile;
815}
816
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700817/*static*/ MediaProfiles::CamcorderProfile*
818MediaProfiles::createDefaultCamcorderCifProfile(camcorder_quality quality)
819{
820 MediaProfiles::VideoCodec *videoCodec =
821 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 360000, 352, 288, 20);
822
823 AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
824 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
825 profile->mCameraId = 0;
826 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
827 profile->mQuality = quality;
828 profile->mDuration = 60;
829 profile->mVideoCodec = videoCodec;
830 profile->mAudioCodec = audioCodec;
831 return profile;
832}
833
834/*static*/ void
835MediaProfiles::createDefaultCamcorderLowProfiles(
836 MediaProfiles::CamcorderProfile **lowProfile,
837 MediaProfiles::CamcorderProfile **lowSpecificProfile) {
838 *lowProfile = createDefaultCamcorderQcifProfile(CAMCORDER_QUALITY_LOW);
839 *lowSpecificProfile = createDefaultCamcorderQcifProfile(CAMCORDER_QUALITY_QCIF);
840}
841
842/*static*/ void
843MediaProfiles::createDefaultCamcorderHighProfiles(
844 MediaProfiles::CamcorderProfile **highProfile,
845 MediaProfiles::CamcorderProfile **highSpecificProfile) {
846 *highProfile = createDefaultCamcorderCifProfile(CAMCORDER_QUALITY_HIGH);
847 *highSpecificProfile = createDefaultCamcorderCifProfile(CAMCORDER_QUALITY_CIF);
848}
849
James Dong1d7491b2010-01-19 17:45:38 -0800850/*static*/ void
851MediaProfiles::createDefaultCamcorderProfiles(MediaProfiles *profiles)
852{
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700853 // low camcorder profiles.
854 MediaProfiles::CamcorderProfile *lowProfile, *lowSpecificProfile;
855 createDefaultCamcorderLowProfiles(&lowProfile, &lowSpecificProfile);
856 profiles->mCamcorderProfiles.add(lowProfile);
857 profiles->mCamcorderProfiles.add(lowSpecificProfile);
858
859 // high camcorder profiles.
860 MediaProfiles::CamcorderProfile* highProfile, *highSpecificProfile;
861 createDefaultCamcorderHighProfiles(&highProfile, &highSpecificProfile);
862 profiles->mCamcorderProfiles.add(highProfile);
863 profiles->mCamcorderProfiles.add(highSpecificProfile);
864
865 // low camcorder time lapse profiles.
866 MediaProfiles::CamcorderProfile *lowTimeLapseProfile, *lowSpecificTimeLapseProfile;
867 createDefaultCamcorderTimeLapseLowProfiles(&lowTimeLapseProfile, &lowSpecificTimeLapseProfile);
868 profiles->mCamcorderProfiles.add(lowTimeLapseProfile);
869 profiles->mCamcorderProfiles.add(lowSpecificTimeLapseProfile);
870
871 // high camcorder time lapse profiles.
872 MediaProfiles::CamcorderProfile *highTimeLapseProfile, *highSpecificTimeLapseProfile;
Glenn Kastenb187de12014-12-30 08:18:15 -0800873 createDefaultCamcorderTimeLapseHighProfiles(&highTimeLapseProfile,
874 &highSpecificTimeLapseProfile);
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700875 profiles->mCamcorderProfiles.add(highTimeLapseProfile);
876 profiles->mCamcorderProfiles.add(highSpecificTimeLapseProfile);
James Dong8031ec72011-03-16 14:09:50 -0700877
878 // For emulator and other legacy devices which does not have a
879 // media_profiles.xml file, We assume that the default camera id
880 // is 0 and that is the only camera available.
881 profiles->mCameraIds.push(0);
James Dong1d7491b2010-01-19 17:45:38 -0800882}
883
884/*static*/ void
885MediaProfiles::createDefaultAudioEncoders(MediaProfiles *profiles)
886{
887 profiles->mAudioEncoders.add(createDefaultAmrNBEncoderCap());
888}
889
890/*static*/ void
891MediaProfiles::createDefaultVideoDecoders(MediaProfiles *profiles)
892{
893 MediaProfiles::VideoDecoderCap *cap =
894 new MediaProfiles::VideoDecoderCap(VIDEO_DECODER_WMV);
895
896 profiles->mVideoDecoders.add(cap);
897}
898
899/*static*/ void
900MediaProfiles::createDefaultAudioDecoders(MediaProfiles *profiles)
901{
902 MediaProfiles::AudioDecoderCap *cap =
903 new MediaProfiles::AudioDecoderCap(AUDIO_DECODER_WMA);
904
905 profiles->mAudioDecoders.add(cap);
906}
907
908/*static*/ void
909MediaProfiles::createDefaultEncoderOutputFileFormats(MediaProfiles *profiles)
910{
911 profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_THREE_GPP);
912 profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_MPEG_4);
913}
914
915/*static*/ MediaProfiles::AudioEncoderCap*
916MediaProfiles::createDefaultAmrNBEncoderCap()
917{
918 return new MediaProfiles::AudioEncoderCap(
919 AUDIO_ENCODER_AMR_NB, 5525, 12200, 8000, 8000, 1, 1);
920}
921
James Dongf5a83852010-02-23 17:21:44 -0800922/*static*/ void
923MediaProfiles::createDefaultImageEncodingQualityLevels(MediaProfiles *profiles)
924{
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800925 ImageEncodingQualityLevels *levels = new ImageEncodingQualityLevels();
926 levels->mCameraId = 0;
927 levels->mLevels.add(70);
928 levels->mLevels.add(80);
929 levels->mLevels.add(90);
930 profiles->mImageEncodingQualityLevels.add(levels);
James Dongf5a83852010-02-23 17:21:44 -0800931}
932
James Dong1d7491b2010-01-19 17:45:38 -0800933/*static*/ MediaProfiles*
934MediaProfiles::createDefaultInstance()
935{
936 MediaProfiles *profiles = new MediaProfiles;
937 createDefaultCamcorderProfiles(profiles);
938 createDefaultVideoEncoders(profiles);
939 createDefaultAudioEncoders(profiles);
940 createDefaultVideoDecoders(profiles);
941 createDefaultAudioDecoders(profiles);
942 createDefaultEncoderOutputFileFormats(profiles);
James Dongf5a83852010-02-23 17:21:44 -0800943 createDefaultImageEncodingQualityLevels(profiles);
James Dong1d7491b2010-01-19 17:45:38 -0800944 return profiles;
945}
946
Pawin Vongmasad7db05b2017-05-03 04:19:20 -0700947bool MediaProfiles::checkXmlFile(const char* xmlFile) {
948 struct stat fStat;
949 return stat(xmlFile, &fStat) == 0 && S_ISREG(fStat.st_mode);
950 // TODO: Add validation
951}
952
James Dong1d7491b2010-01-19 17:45:38 -0800953/*static*/ MediaProfiles*
954MediaProfiles::createInstanceFromXmlFile(const char *xml)
955{
956 FILE *fp = NULL;
957 CHECK((fp = fopen(xml, "r")));
958
959 XML_Parser parser = ::XML_ParserCreate(NULL);
960 CHECK(parser != NULL);
961
962 MediaProfiles *profiles = new MediaProfiles();
963 ::XML_SetUserData(parser, profiles);
964 ::XML_SetElementHandler(parser, startElementHandler, NULL);
965
966 /*
967 FIXME:
968 expat is not compiled with -DXML_DTD. We don't have DTD parsing support.
969
970 if (!::XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS)) {
Steve Block29357bc2012-01-06 19:20:56 +0000971 ALOGE("failed to enable DTD support in the xml file");
James Dong1d7491b2010-01-19 17:45:38 -0800972 return UNKNOWN_ERROR;
973 }
974
975 */
976
977 const int BUFF_SIZE = 512;
978 for (;;) {
979 void *buff = ::XML_GetBuffer(parser, BUFF_SIZE);
980 if (buff == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000981 ALOGE("failed to in call to XML_GetBuffer()");
James Dong1d7491b2010-01-19 17:45:38 -0800982 delete profiles;
983 profiles = NULL;
984 goto exit;
985 }
986
987 int bytes_read = ::fread(buff, 1, BUFF_SIZE, fp);
988 if (bytes_read < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000989 ALOGE("failed in call to read");
James Dong1d7491b2010-01-19 17:45:38 -0800990 delete profiles;
991 profiles = NULL;
992 goto exit;
993 }
994
995 CHECK(::XML_ParseBuffer(parser, bytes_read, bytes_read == 0));
996
997 if (bytes_read == 0) break; // done parsing the xml file
998 }
999
1000exit:
1001 ::XML_ParserFree(parser);
1002 ::fclose(fp);
James Dong1d7491b2010-01-19 17:45:38 -08001003 return profiles;
1004}
1005
1006Vector<output_format> MediaProfiles::getOutputFileFormats() const
1007{
1008 return mEncoderOutputFileFormats; // copy out
1009}
1010
1011Vector<video_encoder> MediaProfiles::getVideoEncoders() const
1012{
1013 Vector<video_encoder> encoders;
1014 for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
1015 encoders.add(mVideoEncoders[i]->mCodec);
1016 }
1017 return encoders; // copy out
1018}
1019
1020int MediaProfiles::getVideoEncoderParamByName(const char *name, video_encoder codec) const
1021{
Steve Block3856b092011-10-20 11:56:00 +01001022 ALOGV("getVideoEncoderParamByName: %s for codec %d", name, codec);
James Dong1d7491b2010-01-19 17:45:38 -08001023 int index = -1;
1024 for (size_t i = 0, n = mVideoEncoders.size(); i < n; ++i) {
1025 if (mVideoEncoders[i]->mCodec == codec) {
1026 index = i;
1027 break;
1028 }
1029 }
1030 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +00001031 ALOGE("The given video encoder %d is not found", codec);
James Dong1d7491b2010-01-19 17:45:38 -08001032 return -1;
1033 }
1034
1035 if (!strcmp("enc.vid.width.min", name)) return mVideoEncoders[index]->mMinFrameWidth;
1036 if (!strcmp("enc.vid.width.max", name)) return mVideoEncoders[index]->mMaxFrameWidth;
1037 if (!strcmp("enc.vid.height.min", name)) return mVideoEncoders[index]->mMinFrameHeight;
1038 if (!strcmp("enc.vid.height.max", name)) return mVideoEncoders[index]->mMaxFrameHeight;
1039 if (!strcmp("enc.vid.bps.min", name)) return mVideoEncoders[index]->mMinBitRate;
1040 if (!strcmp("enc.vid.bps.max", name)) return mVideoEncoders[index]->mMaxBitRate;
1041 if (!strcmp("enc.vid.fps.min", name)) return mVideoEncoders[index]->mMinFrameRate;
1042 if (!strcmp("enc.vid.fps.max", name)) return mVideoEncoders[index]->mMaxFrameRate;
1043
Steve Block29357bc2012-01-06 19:20:56 +00001044 ALOGE("The given video encoder param name %s is not found", name);
James Dong1d7491b2010-01-19 17:45:38 -08001045 return -1;
1046}
Hong Tengcabd5f82011-07-06 18:33:09 -07001047
James Dong1d7491b2010-01-19 17:45:38 -08001048Vector<audio_encoder> MediaProfiles::getAudioEncoders() const
1049{
1050 Vector<audio_encoder> encoders;
1051 for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
1052 encoders.add(mAudioEncoders[i]->mCodec);
1053 }
1054 return encoders; // copy out
1055}
1056
1057int MediaProfiles::getAudioEncoderParamByName(const char *name, audio_encoder codec) const
1058{
Steve Block3856b092011-10-20 11:56:00 +01001059 ALOGV("getAudioEncoderParamByName: %s for codec %d", name, codec);
James Dong1d7491b2010-01-19 17:45:38 -08001060 int index = -1;
1061 for (size_t i = 0, n = mAudioEncoders.size(); i < n; ++i) {
1062 if (mAudioEncoders[i]->mCodec == codec) {
1063 index = i;
1064 break;
1065 }
1066 }
1067 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +00001068 ALOGE("The given audio encoder %d is not found", codec);
James Dong1d7491b2010-01-19 17:45:38 -08001069 return -1;
1070 }
1071
1072 if (!strcmp("enc.aud.ch.min", name)) return mAudioEncoders[index]->mMinChannels;
1073 if (!strcmp("enc.aud.ch.max", name)) return mAudioEncoders[index]->mMaxChannels;
1074 if (!strcmp("enc.aud.bps.min", name)) return mAudioEncoders[index]->mMinBitRate;
1075 if (!strcmp("enc.aud.bps.max", name)) return mAudioEncoders[index]->mMaxBitRate;
1076 if (!strcmp("enc.aud.hz.min", name)) return mAudioEncoders[index]->mMinSampleRate;
1077 if (!strcmp("enc.aud.hz.max", name)) return mAudioEncoders[index]->mMaxSampleRate;
1078
Steve Block29357bc2012-01-06 19:20:56 +00001079 ALOGE("The given audio encoder param name %s is not found", name);
James Dong1d7491b2010-01-19 17:45:38 -08001080 return -1;
1081}
1082
1083Vector<video_decoder> MediaProfiles::getVideoDecoders() const
1084{
1085 Vector<video_decoder> decoders;
1086 for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
1087 decoders.add(mVideoDecoders[i]->mCodec);
1088 }
1089 return decoders; // copy out
1090}
1091
1092Vector<audio_decoder> MediaProfiles::getAudioDecoders() const
1093{
1094 Vector<audio_decoder> decoders;
1095 for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
1096 decoders.add(mAudioDecoders[i]->mCodec);
1097 }
1098 return decoders; // copy out
1099}
1100
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001101int MediaProfiles::getCamcorderProfileIndex(int cameraId, camcorder_quality quality) const
James Dong1d7491b2010-01-19 17:45:38 -08001102{
James Dong1d7491b2010-01-19 17:45:38 -08001103 int index = -1;
1104 for (size_t i = 0, n = mCamcorderProfiles.size(); i < n; ++i) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001105 if (mCamcorderProfiles[i]->mCameraId == cameraId &&
1106 mCamcorderProfiles[i]->mQuality == quality) {
James Dong1d7491b2010-01-19 17:45:38 -08001107 index = i;
1108 break;
1109 }
1110 }
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001111 return index;
1112}
1113
1114int MediaProfiles::getCamcorderProfileParamByName(const char *name,
1115 int cameraId,
1116 camcorder_quality quality) const
1117{
Steve Block3856b092011-10-20 11:56:00 +01001118 ALOGV("getCamcorderProfileParamByName: %s for camera %d, quality %d",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001119 name, cameraId, quality);
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001120
1121 int index = getCamcorderProfileIndex(cameraId, quality);
James Dong1d7491b2010-01-19 17:45:38 -08001122 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +00001123 ALOGE("The given camcorder profile camera %d quality %d is not found",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001124 cameraId, quality);
James Dong1d7491b2010-01-19 17:45:38 -08001125 return -1;
1126 }
1127
James Dongf5a83852010-02-23 17:21:44 -08001128 if (!strcmp("duration", name)) return mCamcorderProfiles[index]->mDuration;
James Dong1d7491b2010-01-19 17:45:38 -08001129 if (!strcmp("file.format", name)) return mCamcorderProfiles[index]->mFileFormat;
1130 if (!strcmp("vid.codec", name)) return mCamcorderProfiles[index]->mVideoCodec->mCodec;
1131 if (!strcmp("vid.width", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameWidth;
1132 if (!strcmp("vid.height", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameHeight;
1133 if (!strcmp("vid.bps", name)) return mCamcorderProfiles[index]->mVideoCodec->mBitRate;
1134 if (!strcmp("vid.fps", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameRate;
1135 if (!strcmp("aud.codec", name)) return mCamcorderProfiles[index]->mAudioCodec->mCodec;
1136 if (!strcmp("aud.bps", name)) return mCamcorderProfiles[index]->mAudioCodec->mBitRate;
1137 if (!strcmp("aud.ch", name)) return mCamcorderProfiles[index]->mAudioCodec->mChannels;
1138 if (!strcmp("aud.hz", name)) return mCamcorderProfiles[index]->mAudioCodec->mSampleRate;
1139
Steve Block29357bc2012-01-06 19:20:56 +00001140 ALOGE("The given camcorder profile param id %d name %s is not found", cameraId, name);
James Dong1d7491b2010-01-19 17:45:38 -08001141 return -1;
1142}
1143
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001144bool MediaProfiles::hasCamcorderProfile(int cameraId, camcorder_quality quality) const
1145{
1146 return (getCamcorderProfileIndex(cameraId, quality) != -1);
1147}
1148
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001149Vector<int> MediaProfiles::getImageEncodingQualityLevels(int cameraId) const
James Dongf5a83852010-02-23 17:21:44 -08001150{
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001151 Vector<int> result;
1152 ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
1153 if (levels != NULL) {
1154 result = levels->mLevels; // copy out
1155 }
1156 return result;
James Dongf5a83852010-02-23 17:21:44 -08001157}
1158
James Dong0f056292011-05-09 18:49:31 -07001159int MediaProfiles::getStartTimeOffsetMs(int cameraId) const {
1160 int offsetTimeMs = -1;
1161 ssize_t index = mStartTimeOffsets.indexOfKey(cameraId);
1162 if (index >= 0) {
1163 offsetTimeMs = mStartTimeOffsets.valueFor(cameraId);
1164 }
Steve Block3856b092011-10-20 11:56:00 +01001165 ALOGV("offsetTime=%d ms and cameraId=%d", offsetTimeMs, cameraId);
James Dong0f056292011-05-09 18:49:31 -07001166 return offsetTimeMs;
1167}
1168
James Dong1d7491b2010-01-19 17:45:38 -08001169MediaProfiles::~MediaProfiles()
1170{
1171 CHECK("destructor should never be called" == 0);
1172#if 0
1173 for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
1174 delete mAudioEncoders[i];
1175 }
1176 mAudioEncoders.clear();
1177
1178 for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
1179 delete mVideoEncoders[i];
1180 }
1181 mVideoEncoders.clear();
1182
1183 for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
1184 delete mVideoDecoders[i];
1185 }
1186 mVideoDecoders.clear();
1187
1188 for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
1189 delete mAudioDecoders[i];
1190 }
1191 mAudioDecoders.clear();
1192
1193 for (size_t i = 0; i < mCamcorderProfiles.size(); ++i) {
1194 delete mCamcorderProfiles[i];
1195 }
1196 mCamcorderProfiles.clear();
1197#endif
1198}
1199} // namespace android