blob: 9c24cbaf1e34af1a44d804a3b9f23351e4bf663c [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,
66 "system/etc/media_profiles_V1_0.xml" // System fallback
67 };
68 }();
69 static std::array<char const*, 5> const cPaths = {
70 paths[0].data(),
71 paths[1].data(),
72 paths[2].data(),
73 paths[3].data(),
74 paths[4].data()
75 };
76 return cPaths;
77}
78
79} // unnamed namespace
80
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
Lajos Molnarb0aeb8b2021-03-24 10:11:41 -0700232/*static*/ void
James Dong1d7491b2010-01-19 17:45:38 -0800233MediaProfiles::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) {
Lajos Molnarb0aeb8b2021-03-24 10:11:41 -0700244 ALOGE("MediaProfiles::createVideoCodec failed to locate codec %s", atts[1]);
245 return;
Alex Zhang032f7a12020-04-17 16:08:24 -0700246 }
James Dong1d7491b2010-01-19 17:45:38 -0800247
Lajos Molnarb0aeb8b2021-03-24 10:11:41 -0700248 VideoCodec videoCodec {
249 static_cast<video_encoder>(codec),
250 atoi(atts[3]), atoi(atts[5]), atoi(atts[7]), atoi(atts[9]) };
251 logVideoCodec(videoCodec);
James Dong1d7491b2010-01-19 17:45:38 -0800252
253 size_t nCamcorderProfiles;
254 CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
Lajos Molnarb0aeb8b2021-03-24 10:11:41 -0700255 profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mVideoCodecs.emplace_back(videoCodec);
James Dong1d7491b2010-01-19 17:45:38 -0800256}
257
Lajos Molnarb0aeb8b2021-03-24 10:11:41 -0700258/*static*/ void
James Dong1d7491b2010-01-19 17:45:38 -0800259MediaProfiles::createAudioCodec(const char **atts, MediaProfiles *profiles)
260{
261 CHECK(!strcmp("codec", atts[0]) &&
262 !strcmp("bitRate", atts[2]) &&
263 !strcmp("sampleRate", atts[4]) &&
264 !strcmp("channels", atts[6]));
265 const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
266 const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
Alex Zhang032f7a12020-04-17 16:08:24 -0700267 if (codec == -1) {
Lajos Molnarb0aeb8b2021-03-24 10:11:41 -0700268 ALOGE("MediaProfiles::createAudioCodec failed to locate codec %s", atts[1]);
269 return;
Alex Zhang032f7a12020-04-17 16:08:24 -0700270 }
James Dong1d7491b2010-01-19 17:45:38 -0800271
Lajos Molnarb0aeb8b2021-03-24 10:11:41 -0700272 AudioCodec audioCodec {
273 static_cast<audio_encoder>(codec),
274 atoi(atts[3]), atoi(atts[5]), atoi(atts[7]) };
275 logAudioCodec(audioCodec);
James Dong1d7491b2010-01-19 17:45:38 -0800276
277 size_t nCamcorderProfiles;
278 CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
Lajos Molnarb0aeb8b2021-03-24 10:11:41 -0700279 profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mAudioCodecs.emplace_back(audioCodec);
James Dong1d7491b2010-01-19 17:45:38 -0800280}
Lajos Molnarb0aeb8b2021-03-24 10:11:41 -0700281
James Dong1d7491b2010-01-19 17:45:38 -0800282/*static*/ MediaProfiles::AudioDecoderCap*
283MediaProfiles::createAudioDecoderCap(const char **atts)
284{
285 CHECK(!strcmp("name", atts[0]) &&
286 !strcmp("enabled", atts[2]));
287
288 const size_t nMappings = sizeof(sAudioDecoderNameMap)/sizeof(sAudioDecoderNameMap[0]);
289 const int codec = findTagForName(sAudioDecoderNameMap, nMappings, atts[1]);
Alex Zhang032f7a12020-04-17 16:08:24 -0700290 if (codec == -1) {
291 ALOGE("MediaProfiles::createAudioDecoderCap failed to locate codec %s", atts[1]);
292 return nullptr;
293 }
James Dong1d7491b2010-01-19 17:45:38 -0800294
295 MediaProfiles::AudioDecoderCap *cap =
296 new MediaProfiles::AudioDecoderCap(static_cast<audio_decoder>(codec));
297 logAudioDecoderCap(*cap);
298 return cap;
299}
300
301/*static*/ MediaProfiles::VideoDecoderCap*
302MediaProfiles::createVideoDecoderCap(const char **atts)
303{
304 CHECK(!strcmp("name", atts[0]) &&
305 !strcmp("enabled", atts[2]));
306
307 const size_t nMappings = sizeof(sVideoDecoderNameMap)/sizeof(sVideoDecoderNameMap[0]);
308 const int codec = findTagForName(sVideoDecoderNameMap, nMappings, atts[1]);
Alex Zhang032f7a12020-04-17 16:08:24 -0700309 if (codec == -1) {
310 ALOGE("MediaProfiles::createVideoDecoderCap failed to locate codec %s", atts[1]);
311 return nullptr;
312 }
James Dong1d7491b2010-01-19 17:45:38 -0800313
314 MediaProfiles::VideoDecoderCap *cap =
315 new MediaProfiles::VideoDecoderCap(static_cast<video_decoder>(codec));
316 logVideoDecoderCap(*cap);
317 return cap;
318}
319
320/*static*/ MediaProfiles::VideoEncoderCap*
321MediaProfiles::createVideoEncoderCap(const char **atts)
322{
323 CHECK(!strcmp("name", atts[0]) &&
324 !strcmp("enabled", atts[2]) &&
325 !strcmp("minBitRate", atts[4]) &&
326 !strcmp("maxBitRate", atts[6]) &&
327 !strcmp("minFrameWidth", atts[8]) &&
328 !strcmp("maxFrameWidth", atts[10]) &&
329 !strcmp("minFrameHeight", atts[12]) &&
330 !strcmp("maxFrameHeight", atts[14]) &&
331 !strcmp("minFrameRate", atts[16]) &&
332 !strcmp("maxFrameRate", atts[18]));
333
334 const size_t nMappings = sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
335 const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
Alex Zhang032f7a12020-04-17 16:08:24 -0700336 if (codec == -1) {
337 ALOGE("MediaProfiles::createVideoEncoderCap failed to locate codec %s", atts[1]);
338 return nullptr;
339 }
James Dong1d7491b2010-01-19 17:45:38 -0800340
341 MediaProfiles::VideoEncoderCap *cap =
342 new MediaProfiles::VideoEncoderCap(static_cast<video_encoder>(codec),
343 atoi(atts[5]), atoi(atts[7]), atoi(atts[9]), atoi(atts[11]), atoi(atts[13]),
344 atoi(atts[15]), atoi(atts[17]), atoi(atts[19]));
345 logVideoEncoderCap(*cap);
346 return cap;
347}
348
349/*static*/ MediaProfiles::AudioEncoderCap*
350MediaProfiles::createAudioEncoderCap(const char **atts)
351{
352 CHECK(!strcmp("name", atts[0]) &&
353 !strcmp("enabled", atts[2]) &&
354 !strcmp("minBitRate", atts[4]) &&
355 !strcmp("maxBitRate", atts[6]) &&
356 !strcmp("minSampleRate", atts[8]) &&
357 !strcmp("maxSampleRate", atts[10]) &&
358 !strcmp("minChannels", atts[12]) &&
359 !strcmp("maxChannels", atts[14]));
360
361 const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
362 const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
Alex Zhang032f7a12020-04-17 16:08:24 -0700363 if (codec == -1) {
364 ALOGE("MediaProfiles::createAudioEncoderCap failed to locate codec %s", atts[1]);
365 return nullptr;
366 }
James Dong1d7491b2010-01-19 17:45:38 -0800367
368 MediaProfiles::AudioEncoderCap *cap =
Glenn Kastenb187de12014-12-30 08:18:15 -0800369 new MediaProfiles::AudioEncoderCap(static_cast<audio_encoder>(codec), atoi(atts[5]),
370 atoi(atts[7]), atoi(atts[9]), atoi(atts[11]), atoi(atts[13]), atoi(atts[15]));
James Dong1d7491b2010-01-19 17:45:38 -0800371 logAudioEncoderCap(*cap);
372 return cap;
373}
374
375/*static*/ output_format
376MediaProfiles::createEncoderOutputFileFormat(const char **atts)
377{
378 CHECK(!strcmp("name", atts[0]));
379
380 const size_t nMappings =sizeof(sFileFormatMap)/sizeof(sFileFormatMap[0]);
381 const int format = findTagForName(sFileFormatMap, nMappings, atts[1]);
382 CHECK(format != -1);
383
384 return static_cast<output_format>(format);
385}
386
James Dong2a7e0a12011-02-28 21:07:39 -0800387static bool isCameraIdFound(int cameraId, const Vector<int>& cameraIds) {
388 for (int i = 0, n = cameraIds.size(); i < n; ++i) {
389 if (cameraId == cameraIds[i]) {
390 return true;
391 }
392 }
393 return false;
394}
395
James Dong1d7491b2010-01-19 17:45:38 -0800396/*static*/ MediaProfiles::CamcorderProfile*
James Dong2a7e0a12011-02-28 21:07:39 -0800397MediaProfiles::createCamcorderProfile(int cameraId, const char **atts, Vector<int>& cameraIds)
James Dong1d7491b2010-01-19 17:45:38 -0800398{
399 CHECK(!strcmp("quality", atts[0]) &&
400 !strcmp("fileFormat", atts[2]) &&
401 !strcmp("duration", atts[4]));
402
Glenn Kastenb187de12014-12-30 08:18:15 -0800403 const size_t nProfileMappings = sizeof(sCamcorderQualityNameMap)/
404 sizeof(sCamcorderQualityNameMap[0]);
James Dong1d7491b2010-01-19 17:45:38 -0800405 const int quality = findTagForName(sCamcorderQualityNameMap, nProfileMappings, atts[1]);
Alex Zhang032f7a12020-04-17 16:08:24 -0700406 if (quality == -1) {
407 ALOGE("MediaProfiles::createCamcorderProfile failed to locate quality %s", atts[1]);
408 return nullptr;
409 }
James Dong1d7491b2010-01-19 17:45:38 -0800410
411 const size_t nFormatMappings = sizeof(sFileFormatMap)/sizeof(sFileFormatMap[0]);
412 const int fileFormat = findTagForName(sFileFormatMap, nFormatMappings, atts[3]);
Alex Zhang032f7a12020-04-17 16:08:24 -0700413 if (fileFormat == -1) {
414 ALOGE("MediaProfiles::createCamcorderProfile failed to locate file format %s", atts[1]);
415 return nullptr;
416 }
James Dong1d7491b2010-01-19 17:45:38 -0800417
418 MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800419 profile->mCameraId = cameraId;
James Dong2a7e0a12011-02-28 21:07:39 -0800420 if (!isCameraIdFound(cameraId, cameraIds)) {
421 cameraIds.add(cameraId);
422 }
James Dong1d7491b2010-01-19 17:45:38 -0800423 profile->mFileFormat = static_cast<output_format>(fileFormat);
424 profile->mQuality = static_cast<camcorder_quality>(quality);
425 profile->mDuration = atoi(atts[5]);
426 return profile;
427}
428
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800429MediaProfiles::ImageEncodingQualityLevels*
430MediaProfiles::findImageEncodingQualityLevels(int cameraId) const
431{
432 int n = mImageEncodingQualityLevels.size();
433 for (int i = 0; i < n; i++) {
434 ImageEncodingQualityLevels *levels = mImageEncodingQualityLevels[i];
435 if (levels->mCameraId == cameraId) {
436 return levels;
437 }
438 }
439 return NULL;
440}
441
442void MediaProfiles::addImageEncodingQualityLevel(int cameraId, const char** atts)
James Dongf5a83852010-02-23 17:21:44 -0800443{
444 CHECK(!strcmp("quality", atts[0]));
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800445 int quality = atoi(atts[1]);
Glenn Kasten90bebef2012-01-27 15:24:38 -0800446 ALOGV("%s: cameraId=%d, quality=%d", __func__, cameraId, quality);
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800447 ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
448
449 if (levels == NULL) {
450 levels = new ImageEncodingQualityLevels();
451 levels->mCameraId = cameraId;
452 mImageEncodingQualityLevels.add(levels);
453 }
454
455 levels->mLevels.add(quality);
456}
457
458/*static*/ int
459MediaProfiles::getCameraId(const char** atts)
460{
461 if (!atts[0]) return 0; // default cameraId = 0
462 CHECK(!strcmp("cameraId", atts[0]));
James Dongf5a83852010-02-23 17:21:44 -0800463 return atoi(atts[1]);
464}
465
James Dong0f056292011-05-09 18:49:31 -0700466void MediaProfiles::addStartTimeOffset(int cameraId, const char** atts)
467{
Eric Laurentb1eb1a02012-10-22 17:44:24 -0700468 int offsetTimeMs = 1000;
James Dong0f056292011-05-09 18:49:31 -0700469 if (atts[2]) {
470 CHECK(!strcmp("startOffsetMs", atts[2]));
471 offsetTimeMs = atoi(atts[3]);
472 }
473
Steve Block3856b092011-10-20 11:56:00 +0100474 ALOGV("%s: cameraId=%d, offset=%d ms", __func__, cameraId, offsetTimeMs);
James Dong0f056292011-05-09 18:49:31 -0700475 mStartTimeOffsets.replaceValueFor(cameraId, offsetTimeMs);
476}
Hong Tengcabd5f82011-07-06 18:33:09 -0700477
James Dong1d7491b2010-01-19 17:45:38 -0800478/*static*/ void
479MediaProfiles::startElementHandler(void *userData, const char *name, const char **atts)
480{
481 MediaProfiles *profiles = (MediaProfiles *) userData;
482 if (strcmp("Video", name) == 0) {
483 createVideoCodec(atts, profiles);
484 } else if (strcmp("Audio", name) == 0) {
485 createAudioCodec(atts, profiles);
486 } else if (strcmp("VideoEncoderCap", name) == 0 &&
487 strcmp("true", atts[3]) == 0) {
Alex Zhang032f7a12020-04-17 16:08:24 -0700488 MediaProfiles::VideoEncoderCap* cap = createVideoEncoderCap(atts);
489 if (cap != nullptr) {
490 profiles->mVideoEncoders.add(cap);
491 }
James Dong1d7491b2010-01-19 17:45:38 -0800492 } else if (strcmp("AudioEncoderCap", name) == 0 &&
493 strcmp("true", atts[3]) == 0) {
Alex Zhang032f7a12020-04-17 16:08:24 -0700494 MediaProfiles::AudioEncoderCap* cap = createAudioEncoderCap(atts);
495 if (cap != nullptr) {
496 profiles->mAudioEncoders.add(cap);
497 }
James Dong1d7491b2010-01-19 17:45:38 -0800498 } else if (strcmp("VideoDecoderCap", name) == 0 &&
499 strcmp("true", atts[3]) == 0) {
Alex Zhang032f7a12020-04-17 16:08:24 -0700500 MediaProfiles::VideoDecoderCap* cap = createVideoDecoderCap(atts);
501 if (cap != nullptr) {
502 profiles->mVideoDecoders.add(cap);
503 }
James Dong1d7491b2010-01-19 17:45:38 -0800504 } else if (strcmp("AudioDecoderCap", name) == 0 &&
505 strcmp("true", atts[3]) == 0) {
Alex Zhang032f7a12020-04-17 16:08:24 -0700506 MediaProfiles::AudioDecoderCap* cap = createAudioDecoderCap(atts);
507 if (cap != nullptr) {
508 profiles->mAudioDecoders.add(cap);
509 }
James Dong1d7491b2010-01-19 17:45:38 -0800510 } else if (strcmp("EncoderOutputFileFormat", name) == 0) {
511 profiles->mEncoderOutputFileFormats.add(createEncoderOutputFileFormat(atts));
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800512 } else if (strcmp("CamcorderProfiles", name) == 0) {
513 profiles->mCurrentCameraId = getCameraId(atts);
James Dong0f056292011-05-09 18:49:31 -0700514 profiles->addStartTimeOffset(profiles->mCurrentCameraId, atts);
James Dong1d7491b2010-01-19 17:45:38 -0800515 } else if (strcmp("EncoderProfile", name) == 0) {
Alex Zhang032f7a12020-04-17 16:08:24 -0700516 MediaProfiles::CamcorderProfile* profile = createCamcorderProfile(
517 profiles->mCurrentCameraId, atts, profiles->mCameraIds);
518 if (profile != nullptr) {
519 profiles->mCamcorderProfiles.add(profile);
520 }
James Dongf5a83852010-02-23 17:21:44 -0800521 } else if (strcmp("ImageEncoding", name) == 0) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800522 profiles->addImageEncodingQualityLevel(profiles->mCurrentCameraId, atts);
James Dong1d7491b2010-01-19 17:45:38 -0800523 }
524}
525
James Dong2a7e0a12011-02-28 21:07:39 -0800526static bool isCamcorderProfile(camcorder_quality quality) {
527 return quality >= CAMCORDER_QUALITY_LIST_START &&
528 quality <= CAMCORDER_QUALITY_LIST_END;
529}
530
531static bool isTimelapseProfile(camcorder_quality quality) {
532 return quality >= CAMCORDER_QUALITY_TIME_LAPSE_LIST_START &&
533 quality <= CAMCORDER_QUALITY_TIME_LAPSE_LIST_END;
534}
535
Zhijun Hee0790972014-07-23 15:17:26 -0700536static bool isHighSpeedProfile(camcorder_quality quality) {
537 return quality >= CAMCORDER_QUALITY_HIGH_SPEED_LIST_START &&
538 quality <= CAMCORDER_QUALITY_HIGH_SPEED_LIST_END;
539}
540
James Dong2a7e0a12011-02-28 21:07:39 -0800541void MediaProfiles::initRequiredProfileRefs(const Vector<int>& cameraIds) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700542 ALOGV("Number of camera ids: %zu", cameraIds.size());
James Dong2a7e0a12011-02-28 21:07:39 -0800543 CHECK(cameraIds.size() > 0);
544 mRequiredProfileRefs = new RequiredProfiles[cameraIds.size()];
545 for (size_t i = 0, n = cameraIds.size(); i < n; ++i) {
546 mRequiredProfileRefs[i].mCameraId = cameraIds[i];
547 for (size_t j = 0; j < kNumRequiredProfiles; ++j) {
548 mRequiredProfileRefs[i].mRefs[j].mHasRefProfile = false;
549 mRequiredProfileRefs[i].mRefs[j].mRefProfileIndex = -1;
550 if ((j & 1) == 0) { // low resolution
551 mRequiredProfileRefs[i].mRefs[j].mResolutionProduct = 0x7FFFFFFF;
552 } else { // high resolution
553 mRequiredProfileRefs[i].mRefs[j].mResolutionProduct = 0;
554 }
555 }
556 }
557}
558
559int MediaProfiles::getRequiredProfileRefIndex(int cameraId) {
560 for (size_t i = 0, n = mCameraIds.size(); i < n; ++i) {
561 if (mCameraIds[i] == cameraId) {
562 return i;
563 }
564 }
565 return -1;
566}
567
568void MediaProfiles::checkAndAddRequiredProfilesIfNecessary() {
569 if (sIsInitialized) {
570 return;
571 }
572
573 initRequiredProfileRefs(mCameraIds);
574
575 for (size_t i = 0, n = mCamcorderProfiles.size(); i < n; ++i) {
Lajos Molnarb0aeb8b2021-03-24 10:11:41 -0700576 // ensure at least one video and audio profile is added
577 if (mCamcorderProfiles[i]->mVideoCodecs.size() == 0) {
578 mCamcorderProfiles[i]->mVideoCodecs.emplace_back(
579 VIDEO_ENCODER_H263, 192000 /* bitrate */,
580 176 /* width */, 144 /* height */, 20 /* frameRate */);
581 }
582 if (mCamcorderProfiles[i]->mAudioCodecs.size() == 0) {
583 mCamcorderProfiles[i]->mAudioCodecs.emplace_back(
584 AUDIO_ENCODER_AMR_NB, 12200 /* bitrate */,
585 8000 /* sampleRate */, 1 /* channels */);
586 }
587
588 int product = mCamcorderProfiles[i]->mVideoCodecs[0].mFrameWidth *
589 mCamcorderProfiles[i]->mVideoCodecs[0].mFrameHeight;
James Dong2a7e0a12011-02-28 21:07:39 -0800590
591 camcorder_quality quality = mCamcorderProfiles[i]->mQuality;
592 int cameraId = mCamcorderProfiles[i]->mCameraId;
593 int index = -1;
594 int refIndex = getRequiredProfileRefIndex(cameraId);
595 CHECK(refIndex != -1);
596 RequiredProfileRefInfo *info;
597 camcorder_quality refQuality;
James Dong2a7e0a12011-02-28 21:07:39 -0800598
Zhijun Hee0790972014-07-23 15:17:26 -0700599 // Check high and low from either camcorder profile, timelapse profile
600 // or high speed profile, but not all of them. Default, check camcorder profile
James Dong2a7e0a12011-02-28 21:07:39 -0800601 size_t j = 0;
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200602 size_t o = 2;
James Dong2a7e0a12011-02-28 21:07:39 -0800603 if (isTimelapseProfile(quality)) {
604 // Check timelapse profile instead.
605 j = 2;
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200606 o = kNumRequiredProfiles;
Zhijun Hee0790972014-07-23 15:17:26 -0700607 } else if (isHighSpeedProfile(quality)) {
608 // Skip the check for high speed profile.
609 continue;
James Dong2a7e0a12011-02-28 21:07:39 -0800610 } else {
611 // Must be camcorder profile.
612 CHECK(isCamcorderProfile(quality));
613 }
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200614 for (; j < o; ++j) {
James Dong2a7e0a12011-02-28 21:07:39 -0800615 info = &(mRequiredProfileRefs[refIndex].mRefs[j]);
616 if ((j % 2 == 0 && product > info->mResolutionProduct) || // low
617 (j % 2 != 0 && product < info->mResolutionProduct)) { // high
618 continue;
619 }
620 switch (j) {
621 case 0:
622 refQuality = CAMCORDER_QUALITY_LOW;
623 break;
624 case 1:
625 refQuality = CAMCORDER_QUALITY_HIGH;
626 break;
627 case 2:
628 refQuality = CAMCORDER_QUALITY_TIME_LAPSE_LOW;
629 break;
630 case 3:
631 refQuality = CAMCORDER_QUALITY_TIME_LAPSE_HIGH;
632 break;
633 default:
634 CHECK(!"Should never reach here");
635 }
636
637 if (!info->mHasRefProfile) {
638 index = getCamcorderProfileIndex(cameraId, refQuality);
639 }
640 if (index == -1) {
641 // New high or low quality profile is found.
642 // Update its reference.
643 info->mHasRefProfile = true;
644 info->mRefProfileIndex = i;
645 info->mResolutionProduct = product;
646 }
647 }
648 }
649
650 for (size_t cameraId = 0; cameraId < mCameraIds.size(); ++cameraId) {
651 for (size_t j = 0; j < kNumRequiredProfiles; ++j) {
652 int refIndex = getRequiredProfileRefIndex(cameraId);
653 CHECK(refIndex != -1);
654 RequiredProfileRefInfo *info =
655 &mRequiredProfileRefs[refIndex].mRefs[j];
656
657 if (info->mHasRefProfile) {
658
George Burgess IV215545b2018-07-25 10:06:30 -0700659 std::unique_ptr<CamcorderProfile> profile =
660 std::make_unique<CamcorderProfile>(
James Dong2a7e0a12011-02-28 21:07:39 -0800661 *mCamcorderProfiles[info->mRefProfileIndex]);
662
663 // Overwrite the quality
664 switch (j % kNumRequiredProfiles) {
665 case 0:
666 profile->mQuality = CAMCORDER_QUALITY_LOW;
667 break;
668 case 1:
669 profile->mQuality = CAMCORDER_QUALITY_HIGH;
670 break;
671 case 2:
672 profile->mQuality = CAMCORDER_QUALITY_TIME_LAPSE_LOW;
673 break;
674 case 3:
675 profile->mQuality = CAMCORDER_QUALITY_TIME_LAPSE_HIGH;
676 break;
677 default:
678 CHECK(!"Should never come here");
679 }
680
681 int index = getCamcorderProfileIndex(cameraId, profile->mQuality);
682 if (index != -1) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700683 ALOGV("Profile quality %d for camera %zu already exists",
James Dong2a7e0a12011-02-28 21:07:39 -0800684 profile->mQuality, cameraId);
685 CHECK(index == refIndex);
686 continue;
687 }
688
689 // Insert the new profile
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700690 ALOGV("Add a profile: quality %d=>%d for camera %zu",
James Dong2a7e0a12011-02-28 21:07:39 -0800691 mCamcorderProfiles[info->mRefProfileIndex]->mQuality,
692 profile->mQuality, cameraId);
693
George Burgess IV215545b2018-07-25 10:06:30 -0700694 mCamcorderProfiles.add(profile.release());
James Dong2a7e0a12011-02-28 21:07:39 -0800695 }
696 }
697 }
698}
699
James Dong1d7491b2010-01-19 17:45:38 -0800700/*static*/ MediaProfiles*
701MediaProfiles::getInstance()
702{
Steve Block3856b092011-10-20 11:56:00 +0100703 ALOGV("getInstance");
James Dong1d7491b2010-01-19 17:45:38 -0800704 Mutex::Autolock lock(sLock);
705 if (!sIsInitialized) {
706 char value[PROPERTY_VALUE_MAX];
707 if (property_get("media.settings.xml", value, NULL) <= 0) {
Pawin Vongmasad7db05b2017-05-03 04:19:20 -0700708 const char* xmlFile = nullptr;
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -0800709 for (auto const& f : getXmlPaths()) {
Pawin Vongmasad7db05b2017-05-03 04:19:20 -0700710 if (checkXmlFile(f)) {
711 xmlFile = f;
712 break;
713 }
714 }
715 if (xmlFile == nullptr) {
716 ALOGW("Could not find a validated xml file. "
717 "Using the default instance instead.");
James Dong1d7491b2010-01-19 17:45:38 -0800718 sInstance = createDefaultInstance();
719 } else {
Pawin Vongmasad7db05b2017-05-03 04:19:20 -0700720 sInstance = createInstanceFromXmlFile(xmlFile);
James Dong1d7491b2010-01-19 17:45:38 -0800721 }
722 } else {
723 sInstance = createInstanceFromXmlFile(value);
724 }
James Dong2a7e0a12011-02-28 21:07:39 -0800725 CHECK(sInstance != NULL);
726 sInstance->checkAndAddRequiredProfilesIfNecessary();
727 sIsInitialized = true;
James Dong1d7491b2010-01-19 17:45:38 -0800728 }
729
730 return sInstance;
731}
732
733/*static*/ MediaProfiles::VideoEncoderCap*
734MediaProfiles::createDefaultH263VideoEncoderCap()
735{
736 return new MediaProfiles::VideoEncoderCap(
737 VIDEO_ENCODER_H263, 192000, 420000, 176, 352, 144, 288, 1, 20);
738}
739
740/*static*/ MediaProfiles::VideoEncoderCap*
741MediaProfiles::createDefaultM4vVideoEncoderCap()
742{
743 return new MediaProfiles::VideoEncoderCap(
744 VIDEO_ENCODER_MPEG_4_SP, 192000, 420000, 176, 352, 144, 288, 1, 20);
745}
746
747
748/*static*/ void
749MediaProfiles::createDefaultVideoEncoders(MediaProfiles *profiles)
750{
751 profiles->mVideoEncoders.add(createDefaultH263VideoEncoderCap());
752 profiles->mVideoEncoders.add(createDefaultM4vVideoEncoderCap());
753}
754
755/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700756MediaProfiles::createDefaultCamcorderTimeLapseQcifProfile(camcorder_quality quality)
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700757{
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700758 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
759 profile->mCameraId = 0;
760 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700761 profile->mQuality = quality;
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700762 profile->mDuration = 60;
Lajos Molnarb0aeb8b2021-03-24 10:11:41 -0700763 profile->mVideoCodecs.emplace_back(
764 VIDEO_ENCODER_H263, 1000000 /* bitrate */,
765 176 /* width */, 144 /* height */, 20 /* frameRate */);
766 profile->mAudioCodecs.emplace_back(
767 AUDIO_ENCODER_AMR_NB, 12200 /* bitrate */,
768 8000 /* sampleRate */, 1 /* channels */);
769
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700770 return profile;
771}
772
773/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700774MediaProfiles::createDefaultCamcorderTimeLapse480pProfile(camcorder_quality quality)
James Dong1d7491b2010-01-19 17:45:38 -0800775{
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800776 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
777 profile->mCameraId = 0;
James Dong1d7491b2010-01-19 17:45:38 -0800778 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700779 profile->mQuality = quality;
James Dong1d7491b2010-01-19 17:45:38 -0800780 profile->mDuration = 60;
Lajos Molnarb0aeb8b2021-03-24 10:11:41 -0700781 profile->mVideoCodecs.emplace_back(
782 VIDEO_ENCODER_H263, 20000000 /* bitrate */,
783 720 /* width */, 480 /* height */, 20 /* frameRate */);
784 profile->mAudioCodecs.emplace_back(
785 AUDIO_ENCODER_AMR_NB, 12200 /* bitrate */,
786 8000 /* sampleRate */, 1 /* channels */);
James Dong1d7491b2010-01-19 17:45:38 -0800787 return profile;
788}
789
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700790/*static*/ void
791MediaProfiles::createDefaultCamcorderTimeLapseLowProfiles(
792 MediaProfiles::CamcorderProfile **lowTimeLapseProfile,
793 MediaProfiles::CamcorderProfile **lowSpecificTimeLapseProfile) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800794 *lowTimeLapseProfile = createDefaultCamcorderTimeLapseQcifProfile(
795 CAMCORDER_QUALITY_TIME_LAPSE_LOW);
796 *lowSpecificTimeLapseProfile = createDefaultCamcorderTimeLapseQcifProfile(
797 CAMCORDER_QUALITY_TIME_LAPSE_QCIF);
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700798}
799
800/*static*/ void
801MediaProfiles::createDefaultCamcorderTimeLapseHighProfiles(
802 MediaProfiles::CamcorderProfile **highTimeLapseProfile,
803 MediaProfiles::CamcorderProfile **highSpecificTimeLapseProfile) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800804 *highTimeLapseProfile = createDefaultCamcorderTimeLapse480pProfile(
805 CAMCORDER_QUALITY_TIME_LAPSE_HIGH);
806 *highSpecificTimeLapseProfile = createDefaultCamcorderTimeLapse480pProfile(
807 CAMCORDER_QUALITY_TIME_LAPSE_480P);
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700808}
809
James Dong1d7491b2010-01-19 17:45:38 -0800810/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700811MediaProfiles::createDefaultCamcorderQcifProfile(camcorder_quality quality)
James Dong1d7491b2010-01-19 17:45:38 -0800812{
Lajos Molnarb0aeb8b2021-03-24 10:11:41 -0700813 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800814 profile->mCameraId = 0;
James Dong1d7491b2010-01-19 17:45:38 -0800815 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700816 profile->mQuality = quality;
James Dong1d7491b2010-01-19 17:45:38 -0800817 profile->mDuration = 30;
Lajos Molnarb0aeb8b2021-03-24 10:11:41 -0700818 profile->mVideoCodecs.emplace_back(
819 VIDEO_ENCODER_H263, 192000 /* bitrate */,
820 176 /* width */, 144 /* height */, 20 /* frameRate */);
821 profile->mAudioCodecs.emplace_back(
822 AUDIO_ENCODER_AMR_NB, 12200 /* bitrate */,
823 8000 /* sampleRate */, 1 /* channels */);
James Dong1d7491b2010-01-19 17:45:38 -0800824 return profile;
825}
826
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700827/*static*/ MediaProfiles::CamcorderProfile*
828MediaProfiles::createDefaultCamcorderCifProfile(camcorder_quality quality)
829{
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700830 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
831 profile->mCameraId = 0;
832 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
833 profile->mQuality = quality;
834 profile->mDuration = 60;
Lajos Molnarb0aeb8b2021-03-24 10:11:41 -0700835 profile->mVideoCodecs.emplace_back(
836 VIDEO_ENCODER_H263, 360000 /* bitrate */,
837 352 /* width */, 288 /* height */, 20 /* frameRate */);
838 profile->mAudioCodecs.emplace_back(
839 AUDIO_ENCODER_AMR_NB, 12200 /* bitrate */,
840 8000 /* sampleRate */, 1 /* channels */);
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700841 return profile;
842}
843
844/*static*/ void
845MediaProfiles::createDefaultCamcorderLowProfiles(
846 MediaProfiles::CamcorderProfile **lowProfile,
847 MediaProfiles::CamcorderProfile **lowSpecificProfile) {
848 *lowProfile = createDefaultCamcorderQcifProfile(CAMCORDER_QUALITY_LOW);
849 *lowSpecificProfile = createDefaultCamcorderQcifProfile(CAMCORDER_QUALITY_QCIF);
850}
851
852/*static*/ void
853MediaProfiles::createDefaultCamcorderHighProfiles(
854 MediaProfiles::CamcorderProfile **highProfile,
855 MediaProfiles::CamcorderProfile **highSpecificProfile) {
856 *highProfile = createDefaultCamcorderCifProfile(CAMCORDER_QUALITY_HIGH);
857 *highSpecificProfile = createDefaultCamcorderCifProfile(CAMCORDER_QUALITY_CIF);
858}
859
James Dong1d7491b2010-01-19 17:45:38 -0800860/*static*/ void
861MediaProfiles::createDefaultCamcorderProfiles(MediaProfiles *profiles)
862{
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700863 // low camcorder profiles.
864 MediaProfiles::CamcorderProfile *lowProfile, *lowSpecificProfile;
865 createDefaultCamcorderLowProfiles(&lowProfile, &lowSpecificProfile);
866 profiles->mCamcorderProfiles.add(lowProfile);
867 profiles->mCamcorderProfiles.add(lowSpecificProfile);
868
869 // high camcorder profiles.
870 MediaProfiles::CamcorderProfile* highProfile, *highSpecificProfile;
871 createDefaultCamcorderHighProfiles(&highProfile, &highSpecificProfile);
872 profiles->mCamcorderProfiles.add(highProfile);
873 profiles->mCamcorderProfiles.add(highSpecificProfile);
874
875 // low camcorder time lapse profiles.
876 MediaProfiles::CamcorderProfile *lowTimeLapseProfile, *lowSpecificTimeLapseProfile;
877 createDefaultCamcorderTimeLapseLowProfiles(&lowTimeLapseProfile, &lowSpecificTimeLapseProfile);
878 profiles->mCamcorderProfiles.add(lowTimeLapseProfile);
879 profiles->mCamcorderProfiles.add(lowSpecificTimeLapseProfile);
880
881 // high camcorder time lapse profiles.
882 MediaProfiles::CamcorderProfile *highTimeLapseProfile, *highSpecificTimeLapseProfile;
Glenn Kastenb187de12014-12-30 08:18:15 -0800883 createDefaultCamcorderTimeLapseHighProfiles(&highTimeLapseProfile,
884 &highSpecificTimeLapseProfile);
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700885 profiles->mCamcorderProfiles.add(highTimeLapseProfile);
886 profiles->mCamcorderProfiles.add(highSpecificTimeLapseProfile);
James Dong8031ec72011-03-16 14:09:50 -0700887
888 // For emulator and other legacy devices which does not have a
889 // media_profiles.xml file, We assume that the default camera id
890 // is 0 and that is the only camera available.
891 profiles->mCameraIds.push(0);
James Dong1d7491b2010-01-19 17:45:38 -0800892}
893
894/*static*/ void
895MediaProfiles::createDefaultAudioEncoders(MediaProfiles *profiles)
896{
897 profiles->mAudioEncoders.add(createDefaultAmrNBEncoderCap());
898}
899
900/*static*/ void
901MediaProfiles::createDefaultVideoDecoders(MediaProfiles *profiles)
902{
903 MediaProfiles::VideoDecoderCap *cap =
904 new MediaProfiles::VideoDecoderCap(VIDEO_DECODER_WMV);
905
906 profiles->mVideoDecoders.add(cap);
907}
908
909/*static*/ void
910MediaProfiles::createDefaultAudioDecoders(MediaProfiles *profiles)
911{
912 MediaProfiles::AudioDecoderCap *cap =
913 new MediaProfiles::AudioDecoderCap(AUDIO_DECODER_WMA);
914
915 profiles->mAudioDecoders.add(cap);
916}
917
918/*static*/ void
919MediaProfiles::createDefaultEncoderOutputFileFormats(MediaProfiles *profiles)
920{
921 profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_THREE_GPP);
922 profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_MPEG_4);
923}
924
925/*static*/ MediaProfiles::AudioEncoderCap*
926MediaProfiles::createDefaultAmrNBEncoderCap()
927{
928 return new MediaProfiles::AudioEncoderCap(
929 AUDIO_ENCODER_AMR_NB, 5525, 12200, 8000, 8000, 1, 1);
930}
931
James Dongf5a83852010-02-23 17:21:44 -0800932/*static*/ void
933MediaProfiles::createDefaultImageEncodingQualityLevels(MediaProfiles *profiles)
934{
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800935 ImageEncodingQualityLevels *levels = new ImageEncodingQualityLevels();
936 levels->mCameraId = 0;
937 levels->mLevels.add(70);
938 levels->mLevels.add(80);
939 levels->mLevels.add(90);
940 profiles->mImageEncodingQualityLevels.add(levels);
James Dongf5a83852010-02-23 17:21:44 -0800941}
942
James Dong1d7491b2010-01-19 17:45:38 -0800943/*static*/ MediaProfiles*
944MediaProfiles::createDefaultInstance()
945{
946 MediaProfiles *profiles = new MediaProfiles;
947 createDefaultCamcorderProfiles(profiles);
948 createDefaultVideoEncoders(profiles);
949 createDefaultAudioEncoders(profiles);
950 createDefaultVideoDecoders(profiles);
951 createDefaultAudioDecoders(profiles);
952 createDefaultEncoderOutputFileFormats(profiles);
James Dongf5a83852010-02-23 17:21:44 -0800953 createDefaultImageEncodingQualityLevels(profiles);
James Dong1d7491b2010-01-19 17:45:38 -0800954 return profiles;
955}
956
Pawin Vongmasad7db05b2017-05-03 04:19:20 -0700957bool MediaProfiles::checkXmlFile(const char* xmlFile) {
958 struct stat fStat;
959 return stat(xmlFile, &fStat) == 0 && S_ISREG(fStat.st_mode);
960 // TODO: Add validation
961}
962
James Dong1d7491b2010-01-19 17:45:38 -0800963/*static*/ MediaProfiles*
964MediaProfiles::createInstanceFromXmlFile(const char *xml)
965{
966 FILE *fp = NULL;
967 CHECK((fp = fopen(xml, "r")));
968
969 XML_Parser parser = ::XML_ParserCreate(NULL);
970 CHECK(parser != NULL);
971
972 MediaProfiles *profiles = new MediaProfiles();
973 ::XML_SetUserData(parser, profiles);
974 ::XML_SetElementHandler(parser, startElementHandler, NULL);
975
976 /*
977 FIXME:
978 expat is not compiled with -DXML_DTD. We don't have DTD parsing support.
979
980 if (!::XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS)) {
Steve Block29357bc2012-01-06 19:20:56 +0000981 ALOGE("failed to enable DTD support in the xml file");
James Dong1d7491b2010-01-19 17:45:38 -0800982 return UNKNOWN_ERROR;
983 }
984
985 */
986
987 const int BUFF_SIZE = 512;
988 for (;;) {
989 void *buff = ::XML_GetBuffer(parser, BUFF_SIZE);
990 if (buff == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000991 ALOGE("failed to in call to XML_GetBuffer()");
James Dong1d7491b2010-01-19 17:45:38 -0800992 delete profiles;
993 profiles = NULL;
994 goto exit;
995 }
996
997 int bytes_read = ::fread(buff, 1, BUFF_SIZE, fp);
998 if (bytes_read < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000999 ALOGE("failed in call to read");
James Dong1d7491b2010-01-19 17:45:38 -08001000 delete profiles;
1001 profiles = NULL;
1002 goto exit;
1003 }
1004
1005 CHECK(::XML_ParseBuffer(parser, bytes_read, bytes_read == 0));
1006
1007 if (bytes_read == 0) break; // done parsing the xml file
1008 }
1009
1010exit:
1011 ::XML_ParserFree(parser);
1012 ::fclose(fp);
James Dong1d7491b2010-01-19 17:45:38 -08001013 return profiles;
1014}
1015
1016Vector<output_format> MediaProfiles::getOutputFileFormats() const
1017{
1018 return mEncoderOutputFileFormats; // copy out
1019}
1020
1021Vector<video_encoder> MediaProfiles::getVideoEncoders() const
1022{
1023 Vector<video_encoder> encoders;
1024 for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
1025 encoders.add(mVideoEncoders[i]->mCodec);
1026 }
1027 return encoders; // copy out
1028}
1029
1030int MediaProfiles::getVideoEncoderParamByName(const char *name, video_encoder codec) const
1031{
Steve Block3856b092011-10-20 11:56:00 +01001032 ALOGV("getVideoEncoderParamByName: %s for codec %d", name, codec);
James Dong1d7491b2010-01-19 17:45:38 -08001033 int index = -1;
1034 for (size_t i = 0, n = mVideoEncoders.size(); i < n; ++i) {
1035 if (mVideoEncoders[i]->mCodec == codec) {
1036 index = i;
1037 break;
1038 }
1039 }
1040 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +00001041 ALOGE("The given video encoder %d is not found", codec);
James Dong1d7491b2010-01-19 17:45:38 -08001042 return -1;
1043 }
1044
1045 if (!strcmp("enc.vid.width.min", name)) return mVideoEncoders[index]->mMinFrameWidth;
1046 if (!strcmp("enc.vid.width.max", name)) return mVideoEncoders[index]->mMaxFrameWidth;
1047 if (!strcmp("enc.vid.height.min", name)) return mVideoEncoders[index]->mMinFrameHeight;
1048 if (!strcmp("enc.vid.height.max", name)) return mVideoEncoders[index]->mMaxFrameHeight;
1049 if (!strcmp("enc.vid.bps.min", name)) return mVideoEncoders[index]->mMinBitRate;
1050 if (!strcmp("enc.vid.bps.max", name)) return mVideoEncoders[index]->mMaxBitRate;
1051 if (!strcmp("enc.vid.fps.min", name)) return mVideoEncoders[index]->mMinFrameRate;
1052 if (!strcmp("enc.vid.fps.max", name)) return mVideoEncoders[index]->mMaxFrameRate;
1053
Steve Block29357bc2012-01-06 19:20:56 +00001054 ALOGE("The given video encoder param name %s is not found", name);
James Dong1d7491b2010-01-19 17:45:38 -08001055 return -1;
1056}
Hong Tengcabd5f82011-07-06 18:33:09 -07001057
James Dong1d7491b2010-01-19 17:45:38 -08001058Vector<audio_encoder> MediaProfiles::getAudioEncoders() const
1059{
1060 Vector<audio_encoder> encoders;
1061 for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
1062 encoders.add(mAudioEncoders[i]->mCodec);
1063 }
1064 return encoders; // copy out
1065}
1066
1067int MediaProfiles::getAudioEncoderParamByName(const char *name, audio_encoder codec) const
1068{
Steve Block3856b092011-10-20 11:56:00 +01001069 ALOGV("getAudioEncoderParamByName: %s for codec %d", name, codec);
James Dong1d7491b2010-01-19 17:45:38 -08001070 int index = -1;
1071 for (size_t i = 0, n = mAudioEncoders.size(); i < n; ++i) {
1072 if (mAudioEncoders[i]->mCodec == codec) {
1073 index = i;
1074 break;
1075 }
1076 }
1077 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +00001078 ALOGE("The given audio encoder %d is not found", codec);
James Dong1d7491b2010-01-19 17:45:38 -08001079 return -1;
1080 }
1081
1082 if (!strcmp("enc.aud.ch.min", name)) return mAudioEncoders[index]->mMinChannels;
1083 if (!strcmp("enc.aud.ch.max", name)) return mAudioEncoders[index]->mMaxChannels;
1084 if (!strcmp("enc.aud.bps.min", name)) return mAudioEncoders[index]->mMinBitRate;
1085 if (!strcmp("enc.aud.bps.max", name)) return mAudioEncoders[index]->mMaxBitRate;
1086 if (!strcmp("enc.aud.hz.min", name)) return mAudioEncoders[index]->mMinSampleRate;
1087 if (!strcmp("enc.aud.hz.max", name)) return mAudioEncoders[index]->mMaxSampleRate;
1088
Steve Block29357bc2012-01-06 19:20:56 +00001089 ALOGE("The given audio encoder param name %s is not found", name);
James Dong1d7491b2010-01-19 17:45:38 -08001090 return -1;
1091}
1092
1093Vector<video_decoder> MediaProfiles::getVideoDecoders() const
1094{
1095 Vector<video_decoder> decoders;
1096 for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
1097 decoders.add(mVideoDecoders[i]->mCodec);
1098 }
1099 return decoders; // copy out
1100}
1101
1102Vector<audio_decoder> MediaProfiles::getAudioDecoders() const
1103{
1104 Vector<audio_decoder> decoders;
1105 for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
1106 decoders.add(mAudioDecoders[i]->mCodec);
1107 }
1108 return decoders; // copy out
1109}
1110
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001111int MediaProfiles::getCamcorderProfileIndex(int cameraId, camcorder_quality quality) const
James Dong1d7491b2010-01-19 17:45:38 -08001112{
James Dong1d7491b2010-01-19 17:45:38 -08001113 int index = -1;
1114 for (size_t i = 0, n = mCamcorderProfiles.size(); i < n; ++i) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001115 if (mCamcorderProfiles[i]->mCameraId == cameraId &&
1116 mCamcorderProfiles[i]->mQuality == quality) {
James Dong1d7491b2010-01-19 17:45:38 -08001117 index = i;
1118 break;
1119 }
1120 }
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001121 return index;
1122}
1123
Lajos Molnard64df6d2021-04-29 23:20:57 -07001124const MediaProfiles::CamcorderProfile *MediaProfiles::getCamcorderProfile(
1125 int cameraId, camcorder_quality quality) const {
1126 int index = getCamcorderProfileIndex(cameraId, quality);
1127 if (index == -1) {
1128 ALOGE("The given camcorder profile camera %d quality %d is not found",
1129 cameraId, quality);
1130 return nullptr;
1131 }
1132
1133 return mCamcorderProfiles[index];
1134}
1135
1136std::vector<const MediaProfiles::AudioCodec *>
1137MediaProfiles::CamcorderProfile::getAudioCodecs() const {
1138 std::vector<const MediaProfiles::AudioCodec *> res;
1139 for (const MediaProfiles::AudioCodec &ac : mAudioCodecs) {
1140 res.push_back(&ac);
1141 }
1142 return res;
1143}
1144
1145std::vector<const MediaProfiles::VideoCodec *>
1146MediaProfiles::CamcorderProfile::getVideoCodecs() const {
1147 std::vector<const MediaProfiles::VideoCodec *> res;
1148 for (const MediaProfiles::VideoCodec &vc : mVideoCodecs) {
1149 res.push_back(&vc);
1150 }
1151 return res;
1152}
1153
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001154int MediaProfiles::getCamcorderProfileParamByName(const char *name,
1155 int cameraId,
1156 camcorder_quality quality) const
1157{
Steve Block3856b092011-10-20 11:56:00 +01001158 ALOGV("getCamcorderProfileParamByName: %s for camera %d, quality %d",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001159 name, cameraId, quality);
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001160
1161 int index = getCamcorderProfileIndex(cameraId, quality);
James Dong1d7491b2010-01-19 17:45:38 -08001162 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +00001163 ALOGE("The given camcorder profile camera %d quality %d is not found",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001164 cameraId, quality);
James Dong1d7491b2010-01-19 17:45:38 -08001165 return -1;
1166 }
1167
James Dongf5a83852010-02-23 17:21:44 -08001168 if (!strcmp("duration", name)) return mCamcorderProfiles[index]->mDuration;
James Dong1d7491b2010-01-19 17:45:38 -08001169 if (!strcmp("file.format", name)) return mCamcorderProfiles[index]->mFileFormat;
Lajos Molnarb0aeb8b2021-03-24 10:11:41 -07001170 if (!strcmp("vid.codec", name)) return mCamcorderProfiles[index]->mVideoCodecs[0].mCodec;
1171 if (!strcmp("vid.width", name)) return mCamcorderProfiles[index]->mVideoCodecs[0].mFrameWidth;
1172 if (!strcmp("vid.height", name)) return mCamcorderProfiles[index]->mVideoCodecs[0].mFrameHeight;
1173 if (!strcmp("vid.bps", name)) return mCamcorderProfiles[index]->mVideoCodecs[0].mBitRate;
1174 if (!strcmp("vid.fps", name)) return mCamcorderProfiles[index]->mVideoCodecs[0].mFrameRate;
1175 if (!strcmp("aud.codec", name)) return mCamcorderProfiles[index]->mAudioCodecs[0].mCodec;
1176 if (!strcmp("aud.bps", name)) return mCamcorderProfiles[index]->mAudioCodecs[0].mBitRate;
1177 if (!strcmp("aud.ch", name)) return mCamcorderProfiles[index]->mAudioCodecs[0].mChannels;
1178 if (!strcmp("aud.hz", name)) return mCamcorderProfiles[index]->mAudioCodecs[0].mSampleRate;
James Dong1d7491b2010-01-19 17:45:38 -08001179
Steve Block29357bc2012-01-06 19:20:56 +00001180 ALOGE("The given camcorder profile param id %d name %s is not found", cameraId, name);
James Dong1d7491b2010-01-19 17:45:38 -08001181 return -1;
1182}
1183
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001184bool MediaProfiles::hasCamcorderProfile(int cameraId, camcorder_quality quality) const
1185{
1186 return (getCamcorderProfileIndex(cameraId, quality) != -1);
1187}
1188
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001189Vector<int> MediaProfiles::getImageEncodingQualityLevels(int cameraId) const
James Dongf5a83852010-02-23 17:21:44 -08001190{
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001191 Vector<int> result;
1192 ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
1193 if (levels != NULL) {
1194 result = levels->mLevels; // copy out
1195 }
1196 return result;
James Dongf5a83852010-02-23 17:21:44 -08001197}
1198
James Dong0f056292011-05-09 18:49:31 -07001199int MediaProfiles::getStartTimeOffsetMs(int cameraId) const {
1200 int offsetTimeMs = -1;
1201 ssize_t index = mStartTimeOffsets.indexOfKey(cameraId);
1202 if (index >= 0) {
1203 offsetTimeMs = mStartTimeOffsets.valueFor(cameraId);
1204 }
Steve Block3856b092011-10-20 11:56:00 +01001205 ALOGV("offsetTime=%d ms and cameraId=%d", offsetTimeMs, cameraId);
James Dong0f056292011-05-09 18:49:31 -07001206 return offsetTimeMs;
1207}
1208
James Dong1d7491b2010-01-19 17:45:38 -08001209MediaProfiles::~MediaProfiles()
1210{
1211 CHECK("destructor should never be called" == 0);
1212#if 0
1213 for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
1214 delete mAudioEncoders[i];
1215 }
1216 mAudioEncoders.clear();
1217
1218 for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
1219 delete mVideoEncoders[i];
1220 }
1221 mVideoEncoders.clear();
1222
1223 for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
1224 delete mVideoDecoders[i];
1225 }
1226 mVideoDecoders.clear();
1227
1228 for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
1229 delete mAudioDecoders[i];
1230 }
1231 mAudioDecoders.clear();
1232
1233 for (size_t i = 0; i < mCamcorderProfiles.size(); ++i) {
1234 delete mCamcorderProfiles[i];
1235 }
1236 mCamcorderProfiles.clear();
1237#endif
1238}
1239} // namespace android