blob: 1705c97aa721e04ec57f21ee541769eb4eac0948 [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},
Lajos Molnar5e35f772021-02-06 00:33:07 -0800124 {"vga", CAMCORDER_QUALITY_VGA},
125 {"4kdci", CAMCORDER_QUALITY_4KDCI},
126 {"qhd", CAMCORDER_QUALITY_QHD},
127 {"2k", CAMCORDER_QUALITY_2K},
128 {"8kuhd", CAMCORDER_QUALITY_8KUHD},
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700129
130 {"timelapselow", CAMCORDER_QUALITY_TIME_LAPSE_LOW},
131 {"timelapsehigh", CAMCORDER_QUALITY_TIME_LAPSE_HIGH},
132 {"timelapseqcif", CAMCORDER_QUALITY_TIME_LAPSE_QCIF},
Nipun Kwatra9783ed82010-09-10 15:45:57 -0700133 {"timelapsecif", CAMCORDER_QUALITY_TIME_LAPSE_CIF},
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700134 {"timelapse480p", CAMCORDER_QUALITY_TIME_LAPSE_480P},
135 {"timelapse720p", CAMCORDER_QUALITY_TIME_LAPSE_720P},
James Dong669012d2011-09-19 16:27:31 -0700136 {"timelapse1080p", CAMCORDER_QUALITY_TIME_LAPSE_1080P},
Zhijun He5f6af1a2014-06-10 08:26:33 -0700137 {"timelapse2160p", CAMCORDER_QUALITY_TIME_LAPSE_2160P},
James Dong669012d2011-09-19 16:27:31 -0700138 {"timelapseqvga", CAMCORDER_QUALITY_TIME_LAPSE_QVGA},
Lajos Molnar5e35f772021-02-06 00:33:07 -0800139 {"timelapsevga", CAMCORDER_QUALITY_TIME_LAPSE_VGA},
140 {"timelapse4kdci", CAMCORDER_QUALITY_TIME_LAPSE_4KDCI},
141 {"timelapseqhd", CAMCORDER_QUALITY_TIME_LAPSE_QHD},
142 {"timelapse2k", CAMCORDER_QUALITY_TIME_LAPSE_2K},
Zhijun Hee0790972014-07-23 15:17:26 -0700143
144 {"highspeedlow", CAMCORDER_QUALITY_HIGH_SPEED_LOW},
145 {"highspeedhigh", CAMCORDER_QUALITY_HIGH_SPEED_HIGH},
146 {"highspeed480p", CAMCORDER_QUALITY_HIGH_SPEED_480P},
147 {"highspeed720p", CAMCORDER_QUALITY_HIGH_SPEED_720P},
148 {"highspeed1080p", CAMCORDER_QUALITY_HIGH_SPEED_1080P},
Zhijun He9520aa62014-09-09 16:18:31 -0700149 {"highspeed2160p", CAMCORDER_QUALITY_HIGH_SPEED_2160P},
Praveen Chavanb5bfa0e2019-01-16 15:49:42 -0800150 {"highspeedcif", CAMCORDER_QUALITY_HIGH_SPEED_CIF},
151 {"highspeedvga", CAMCORDER_QUALITY_HIGH_SPEED_VGA},
152 {"highspeed4kdci", CAMCORDER_QUALITY_HIGH_SPEED_4KDCI},
Lajos Molnar5e35f772021-02-06 00:33:07 -0800153
154 // Vendor-specific profiles
James Dong1d7491b2010-01-19 17:45:38 -0800155};
156
Glenn Kasten80520382014-01-31 16:49:31 -0800157#if LOG_NDEBUG
158#define UNUSED __unused
159#else
160#define UNUSED
161#endif
162
James Dong1d7491b2010-01-19 17:45:38 -0800163/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800164MediaProfiles::logVideoCodec(const MediaProfiles::VideoCodec& codec UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800165{
Steve Block3856b092011-10-20 11:56:00 +0100166 ALOGV("video codec:");
167 ALOGV("codec = %d", codec.mCodec);
168 ALOGV("bit rate: %d", codec.mBitRate);
169 ALOGV("frame width: %d", codec.mFrameWidth);
170 ALOGV("frame height: %d", codec.mFrameHeight);
171 ALOGV("frame rate: %d", codec.mFrameRate);
James Dong1d7491b2010-01-19 17:45:38 -0800172}
173
174/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800175MediaProfiles::logAudioCodec(const MediaProfiles::AudioCodec& codec UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800176{
Steve Block3856b092011-10-20 11:56:00 +0100177 ALOGV("audio codec:");
178 ALOGV("codec = %d", codec.mCodec);
179 ALOGV("bit rate: %d", codec.mBitRate);
180 ALOGV("sample rate: %d", codec.mSampleRate);
181 ALOGV("number of channels: %d", codec.mChannels);
James Dong1d7491b2010-01-19 17:45:38 -0800182}
183
184/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800185MediaProfiles::logVideoEncoderCap(const MediaProfiles::VideoEncoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800186{
Steve Block3856b092011-10-20 11:56:00 +0100187 ALOGV("video encoder cap:");
188 ALOGV("codec = %d", cap.mCodec);
189 ALOGV("bit rate: min = %d and max = %d", cap.mMinBitRate, cap.mMaxBitRate);
190 ALOGV("frame width: min = %d and max = %d", cap.mMinFrameWidth, cap.mMaxFrameWidth);
191 ALOGV("frame height: min = %d and max = %d", cap.mMinFrameHeight, cap.mMaxFrameHeight);
192 ALOGV("frame rate: min = %d and max = %d", cap.mMinFrameRate, cap.mMaxFrameRate);
James Dong1d7491b2010-01-19 17:45:38 -0800193}
194
195/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800196MediaProfiles::logAudioEncoderCap(const MediaProfiles::AudioEncoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800197{
Steve Block3856b092011-10-20 11:56:00 +0100198 ALOGV("audio encoder cap:");
199 ALOGV("codec = %d", cap.mCodec);
200 ALOGV("bit rate: min = %d and max = %d", cap.mMinBitRate, cap.mMaxBitRate);
201 ALOGV("sample rate: min = %d and max = %d", cap.mMinSampleRate, cap.mMaxSampleRate);
202 ALOGV("number of channels: min = %d and max = %d", cap.mMinChannels, cap.mMaxChannels);
James Dong1d7491b2010-01-19 17:45:38 -0800203}
204
205/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800206MediaProfiles::logVideoDecoderCap(const MediaProfiles::VideoDecoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800207{
Steve Block3856b092011-10-20 11:56:00 +0100208 ALOGV("video decoder cap:");
209 ALOGV("codec = %d", cap.mCodec);
James Dong1d7491b2010-01-19 17:45:38 -0800210}
211
212/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800213MediaProfiles::logAudioDecoderCap(const MediaProfiles::AudioDecoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800214{
Steve Block3856b092011-10-20 11:56:00 +0100215 ALOGV("audio codec cap:");
216 ALOGV("codec = %d", cap.mCodec);
James Dong1d7491b2010-01-19 17:45:38 -0800217}
218
219/*static*/ int
Glenn Kastenb187de12014-12-30 08:18:15 -0800220MediaProfiles::findTagForName(const MediaProfiles::NameToTagMap *map, size_t nMappings,
221 const char *name)
James Dong1d7491b2010-01-19 17:45:38 -0800222{
223 int tag = -1;
224 for (size_t i = 0; i < nMappings; ++i) {
225 if (!strcmp(map[i].name, name)) {
226 tag = map[i].tag;
227 break;
228 }
229 }
230 return tag;
231}
232
233/*static*/ MediaProfiles::VideoCodec*
234MediaProfiles::createVideoCodec(const char **atts, MediaProfiles *profiles)
235{
236 CHECK(!strcmp("codec", atts[0]) &&
237 !strcmp("bitRate", atts[2]) &&
238 !strcmp("width", atts[4]) &&
239 !strcmp("height", atts[6]) &&
240 !strcmp("frameRate", atts[8]));
241
242 const size_t nMappings = sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
243 const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
Alex Zhang032f7a12020-04-17 16:08:24 -0700244 if (codec == -1) {
245 ALOGE("MediaProfiles::createVideoCodec failed to locate codec %s", atts[1]);
246 return nullptr;
247 }
James Dong1d7491b2010-01-19 17:45:38 -0800248
249 MediaProfiles::VideoCodec *videoCodec =
250 new MediaProfiles::VideoCodec(static_cast<video_encoder>(codec),
251 atoi(atts[3]), atoi(atts[5]), atoi(atts[7]), atoi(atts[9]));
252 logVideoCodec(*videoCodec);
253
254 size_t nCamcorderProfiles;
255 CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
256 profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mVideoCodec = videoCodec;
257 return videoCodec;
258}
259
260/*static*/ MediaProfiles::AudioCodec*
261MediaProfiles::createAudioCodec(const char **atts, MediaProfiles *profiles)
262{
263 CHECK(!strcmp("codec", atts[0]) &&
264 !strcmp("bitRate", atts[2]) &&
265 !strcmp("sampleRate", atts[4]) &&
266 !strcmp("channels", atts[6]));
267 const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
268 const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
Alex Zhang032f7a12020-04-17 16:08:24 -0700269 if (codec == -1) {
270 ALOGE("MediaProfiles::createAudioCodec failed to locate codec %s", atts[1]);
271 return nullptr;
272 }
James Dong1d7491b2010-01-19 17:45:38 -0800273
274 MediaProfiles::AudioCodec *audioCodec =
275 new MediaProfiles::AudioCodec(static_cast<audio_encoder>(codec),
276 atoi(atts[3]), atoi(atts[5]), atoi(atts[7]));
277 logAudioCodec(*audioCodec);
278
279 size_t nCamcorderProfiles;
280 CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
281 profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mAudioCodec = audioCodec;
282 return audioCodec;
283}
284/*static*/ MediaProfiles::AudioDecoderCap*
285MediaProfiles::createAudioDecoderCap(const char **atts)
286{
287 CHECK(!strcmp("name", atts[0]) &&
288 !strcmp("enabled", atts[2]));
289
290 const size_t nMappings = sizeof(sAudioDecoderNameMap)/sizeof(sAudioDecoderNameMap[0]);
291 const int codec = findTagForName(sAudioDecoderNameMap, nMappings, atts[1]);
Alex Zhang032f7a12020-04-17 16:08:24 -0700292 if (codec == -1) {
293 ALOGE("MediaProfiles::createAudioDecoderCap failed to locate codec %s", atts[1]);
294 return nullptr;
295 }
James Dong1d7491b2010-01-19 17:45:38 -0800296
297 MediaProfiles::AudioDecoderCap *cap =
298 new MediaProfiles::AudioDecoderCap(static_cast<audio_decoder>(codec));
299 logAudioDecoderCap(*cap);
300 return cap;
301}
302
303/*static*/ MediaProfiles::VideoDecoderCap*
304MediaProfiles::createVideoDecoderCap(const char **atts)
305{
306 CHECK(!strcmp("name", atts[0]) &&
307 !strcmp("enabled", atts[2]));
308
309 const size_t nMappings = sizeof(sVideoDecoderNameMap)/sizeof(sVideoDecoderNameMap[0]);
310 const int codec = findTagForName(sVideoDecoderNameMap, nMappings, atts[1]);
Alex Zhang032f7a12020-04-17 16:08:24 -0700311 if (codec == -1) {
312 ALOGE("MediaProfiles::createVideoDecoderCap failed to locate codec %s", atts[1]);
313 return nullptr;
314 }
James Dong1d7491b2010-01-19 17:45:38 -0800315
316 MediaProfiles::VideoDecoderCap *cap =
317 new MediaProfiles::VideoDecoderCap(static_cast<video_decoder>(codec));
318 logVideoDecoderCap(*cap);
319 return cap;
320}
321
322/*static*/ MediaProfiles::VideoEncoderCap*
323MediaProfiles::createVideoEncoderCap(const char **atts)
324{
325 CHECK(!strcmp("name", atts[0]) &&
326 !strcmp("enabled", atts[2]) &&
327 !strcmp("minBitRate", atts[4]) &&
328 !strcmp("maxBitRate", atts[6]) &&
329 !strcmp("minFrameWidth", atts[8]) &&
330 !strcmp("maxFrameWidth", atts[10]) &&
331 !strcmp("minFrameHeight", atts[12]) &&
332 !strcmp("maxFrameHeight", atts[14]) &&
333 !strcmp("minFrameRate", atts[16]) &&
334 !strcmp("maxFrameRate", atts[18]));
335
336 const size_t nMappings = sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
337 const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
Alex Zhang032f7a12020-04-17 16:08:24 -0700338 if (codec == -1) {
339 ALOGE("MediaProfiles::createVideoEncoderCap failed to locate codec %s", atts[1]);
340 return nullptr;
341 }
James Dong1d7491b2010-01-19 17:45:38 -0800342
343 MediaProfiles::VideoEncoderCap *cap =
344 new MediaProfiles::VideoEncoderCap(static_cast<video_encoder>(codec),
345 atoi(atts[5]), atoi(atts[7]), atoi(atts[9]), atoi(atts[11]), atoi(atts[13]),
346 atoi(atts[15]), atoi(atts[17]), atoi(atts[19]));
347 logVideoEncoderCap(*cap);
348 return cap;
349}
350
351/*static*/ MediaProfiles::AudioEncoderCap*
352MediaProfiles::createAudioEncoderCap(const char **atts)
353{
354 CHECK(!strcmp("name", atts[0]) &&
355 !strcmp("enabled", atts[2]) &&
356 !strcmp("minBitRate", atts[4]) &&
357 !strcmp("maxBitRate", atts[6]) &&
358 !strcmp("minSampleRate", atts[8]) &&
359 !strcmp("maxSampleRate", atts[10]) &&
360 !strcmp("minChannels", atts[12]) &&
361 !strcmp("maxChannels", atts[14]));
362
363 const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
364 const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
Alex Zhang032f7a12020-04-17 16:08:24 -0700365 if (codec == -1) {
366 ALOGE("MediaProfiles::createAudioEncoderCap failed to locate codec %s", atts[1]);
367 return nullptr;
368 }
James Dong1d7491b2010-01-19 17:45:38 -0800369
370 MediaProfiles::AudioEncoderCap *cap =
Glenn Kastenb187de12014-12-30 08:18:15 -0800371 new MediaProfiles::AudioEncoderCap(static_cast<audio_encoder>(codec), atoi(atts[5]),
372 atoi(atts[7]), atoi(atts[9]), atoi(atts[11]), atoi(atts[13]), atoi(atts[15]));
James Dong1d7491b2010-01-19 17:45:38 -0800373 logAudioEncoderCap(*cap);
374 return cap;
375}
376
377/*static*/ output_format
378MediaProfiles::createEncoderOutputFileFormat(const char **atts)
379{
380 CHECK(!strcmp("name", atts[0]));
381
382 const size_t nMappings =sizeof(sFileFormatMap)/sizeof(sFileFormatMap[0]);
383 const int format = findTagForName(sFileFormatMap, nMappings, atts[1]);
384 CHECK(format != -1);
385
386 return static_cast<output_format>(format);
387}
388
James Dong2a7e0a12011-02-28 21:07:39 -0800389static bool isCameraIdFound(int cameraId, const Vector<int>& cameraIds) {
390 for (int i = 0, n = cameraIds.size(); i < n; ++i) {
391 if (cameraId == cameraIds[i]) {
392 return true;
393 }
394 }
395 return false;
396}
397
James Dong1d7491b2010-01-19 17:45:38 -0800398/*static*/ MediaProfiles::CamcorderProfile*
James Dong2a7e0a12011-02-28 21:07:39 -0800399MediaProfiles::createCamcorderProfile(int cameraId, const char **atts, Vector<int>& cameraIds)
James Dong1d7491b2010-01-19 17:45:38 -0800400{
401 CHECK(!strcmp("quality", atts[0]) &&
402 !strcmp("fileFormat", atts[2]) &&
403 !strcmp("duration", atts[4]));
404
Glenn Kastenb187de12014-12-30 08:18:15 -0800405 const size_t nProfileMappings = sizeof(sCamcorderQualityNameMap)/
406 sizeof(sCamcorderQualityNameMap[0]);
James Dong1d7491b2010-01-19 17:45:38 -0800407 const int quality = findTagForName(sCamcorderQualityNameMap, nProfileMappings, atts[1]);
Alex Zhang032f7a12020-04-17 16:08:24 -0700408 if (quality == -1) {
409 ALOGE("MediaProfiles::createCamcorderProfile failed to locate quality %s", atts[1]);
410 return nullptr;
411 }
James Dong1d7491b2010-01-19 17:45:38 -0800412
413 const size_t nFormatMappings = sizeof(sFileFormatMap)/sizeof(sFileFormatMap[0]);
414 const int fileFormat = findTagForName(sFileFormatMap, nFormatMappings, atts[3]);
Alex Zhang032f7a12020-04-17 16:08:24 -0700415 if (fileFormat == -1) {
416 ALOGE("MediaProfiles::createCamcorderProfile failed to locate file format %s", atts[1]);
417 return nullptr;
418 }
James Dong1d7491b2010-01-19 17:45:38 -0800419
420 MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800421 profile->mCameraId = cameraId;
James Dong2a7e0a12011-02-28 21:07:39 -0800422 if (!isCameraIdFound(cameraId, cameraIds)) {
423 cameraIds.add(cameraId);
424 }
James Dong1d7491b2010-01-19 17:45:38 -0800425 profile->mFileFormat = static_cast<output_format>(fileFormat);
426 profile->mQuality = static_cast<camcorder_quality>(quality);
427 profile->mDuration = atoi(atts[5]);
428 return profile;
429}
430
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800431MediaProfiles::ImageEncodingQualityLevels*
432MediaProfiles::findImageEncodingQualityLevels(int cameraId) const
433{
434 int n = mImageEncodingQualityLevels.size();
435 for (int i = 0; i < n; i++) {
436 ImageEncodingQualityLevels *levels = mImageEncodingQualityLevels[i];
437 if (levels->mCameraId == cameraId) {
438 return levels;
439 }
440 }
441 return NULL;
442}
443
444void MediaProfiles::addImageEncodingQualityLevel(int cameraId, const char** atts)
James Dongf5a83852010-02-23 17:21:44 -0800445{
446 CHECK(!strcmp("quality", atts[0]));
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800447 int quality = atoi(atts[1]);
Glenn Kasten90bebef2012-01-27 15:24:38 -0800448 ALOGV("%s: cameraId=%d, quality=%d", __func__, cameraId, quality);
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800449 ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
450
451 if (levels == NULL) {
452 levels = new ImageEncodingQualityLevels();
453 levels->mCameraId = cameraId;
454 mImageEncodingQualityLevels.add(levels);
455 }
456
457 levels->mLevels.add(quality);
458}
459
460/*static*/ int
461MediaProfiles::getCameraId(const char** atts)
462{
463 if (!atts[0]) return 0; // default cameraId = 0
464 CHECK(!strcmp("cameraId", atts[0]));
James Dongf5a83852010-02-23 17:21:44 -0800465 return atoi(atts[1]);
466}
467
James Dong0f056292011-05-09 18:49:31 -0700468void MediaProfiles::addStartTimeOffset(int cameraId, const char** atts)
469{
Eric Laurentb1eb1a02012-10-22 17:44:24 -0700470 int offsetTimeMs = 1000;
James Dong0f056292011-05-09 18:49:31 -0700471 if (atts[2]) {
472 CHECK(!strcmp("startOffsetMs", atts[2]));
473 offsetTimeMs = atoi(atts[3]);
474 }
475
Steve Block3856b092011-10-20 11:56:00 +0100476 ALOGV("%s: cameraId=%d, offset=%d ms", __func__, cameraId, offsetTimeMs);
James Dong0f056292011-05-09 18:49:31 -0700477 mStartTimeOffsets.replaceValueFor(cameraId, offsetTimeMs);
478}
Hong Tengcabd5f82011-07-06 18:33:09 -0700479
James Dong1d7491b2010-01-19 17:45:38 -0800480/*static*/ void
481MediaProfiles::startElementHandler(void *userData, const char *name, const char **atts)
482{
483 MediaProfiles *profiles = (MediaProfiles *) userData;
484 if (strcmp("Video", name) == 0) {
485 createVideoCodec(atts, profiles);
486 } else if (strcmp("Audio", name) == 0) {
487 createAudioCodec(atts, profiles);
488 } else if (strcmp("VideoEncoderCap", name) == 0 &&
489 strcmp("true", atts[3]) == 0) {
Alex Zhang032f7a12020-04-17 16:08:24 -0700490 MediaProfiles::VideoEncoderCap* cap = createVideoEncoderCap(atts);
491 if (cap != nullptr) {
492 profiles->mVideoEncoders.add(cap);
493 }
James Dong1d7491b2010-01-19 17:45:38 -0800494 } else if (strcmp("AudioEncoderCap", name) == 0 &&
495 strcmp("true", atts[3]) == 0) {
Alex Zhang032f7a12020-04-17 16:08:24 -0700496 MediaProfiles::AudioEncoderCap* cap = createAudioEncoderCap(atts);
497 if (cap != nullptr) {
498 profiles->mAudioEncoders.add(cap);
499 }
James Dong1d7491b2010-01-19 17:45:38 -0800500 } else if (strcmp("VideoDecoderCap", name) == 0 &&
501 strcmp("true", atts[3]) == 0) {
Alex Zhang032f7a12020-04-17 16:08:24 -0700502 MediaProfiles::VideoDecoderCap* cap = createVideoDecoderCap(atts);
503 if (cap != nullptr) {
504 profiles->mVideoDecoders.add(cap);
505 }
James Dong1d7491b2010-01-19 17:45:38 -0800506 } else if (strcmp("AudioDecoderCap", name) == 0 &&
507 strcmp("true", atts[3]) == 0) {
Alex Zhang032f7a12020-04-17 16:08:24 -0700508 MediaProfiles::AudioDecoderCap* cap = createAudioDecoderCap(atts);
509 if (cap != nullptr) {
510 profiles->mAudioDecoders.add(cap);
511 }
James Dong1d7491b2010-01-19 17:45:38 -0800512 } else if (strcmp("EncoderOutputFileFormat", name) == 0) {
513 profiles->mEncoderOutputFileFormats.add(createEncoderOutputFileFormat(atts));
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800514 } else if (strcmp("CamcorderProfiles", name) == 0) {
515 profiles->mCurrentCameraId = getCameraId(atts);
James Dong0f056292011-05-09 18:49:31 -0700516 profiles->addStartTimeOffset(profiles->mCurrentCameraId, atts);
James Dong1d7491b2010-01-19 17:45:38 -0800517 } else if (strcmp("EncoderProfile", name) == 0) {
Alex Zhang032f7a12020-04-17 16:08:24 -0700518 MediaProfiles::CamcorderProfile* profile = createCamcorderProfile(
519 profiles->mCurrentCameraId, atts, profiles->mCameraIds);
520 if (profile != nullptr) {
521 profiles->mCamcorderProfiles.add(profile);
522 }
James Dongf5a83852010-02-23 17:21:44 -0800523 } else if (strcmp("ImageEncoding", name) == 0) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800524 profiles->addImageEncodingQualityLevel(profiles->mCurrentCameraId, atts);
James Dong1d7491b2010-01-19 17:45:38 -0800525 }
526}
527
James Dong2a7e0a12011-02-28 21:07:39 -0800528static bool isCamcorderProfile(camcorder_quality quality) {
529 return quality >= CAMCORDER_QUALITY_LIST_START &&
530 quality <= CAMCORDER_QUALITY_LIST_END;
531}
532
533static bool isTimelapseProfile(camcorder_quality quality) {
534 return quality >= CAMCORDER_QUALITY_TIME_LAPSE_LIST_START &&
535 quality <= CAMCORDER_QUALITY_TIME_LAPSE_LIST_END;
536}
537
Zhijun Hee0790972014-07-23 15:17:26 -0700538static bool isHighSpeedProfile(camcorder_quality quality) {
539 return quality >= CAMCORDER_QUALITY_HIGH_SPEED_LIST_START &&
540 quality <= CAMCORDER_QUALITY_HIGH_SPEED_LIST_END;
541}
542
James Dong2a7e0a12011-02-28 21:07:39 -0800543void MediaProfiles::initRequiredProfileRefs(const Vector<int>& cameraIds) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700544 ALOGV("Number of camera ids: %zu", cameraIds.size());
James Dong2a7e0a12011-02-28 21:07:39 -0800545 CHECK(cameraIds.size() > 0);
546 mRequiredProfileRefs = new RequiredProfiles[cameraIds.size()];
547 for (size_t i = 0, n = cameraIds.size(); i < n; ++i) {
548 mRequiredProfileRefs[i].mCameraId = cameraIds[i];
549 for (size_t j = 0; j < kNumRequiredProfiles; ++j) {
550 mRequiredProfileRefs[i].mRefs[j].mHasRefProfile = false;
551 mRequiredProfileRefs[i].mRefs[j].mRefProfileIndex = -1;
552 if ((j & 1) == 0) { // low resolution
553 mRequiredProfileRefs[i].mRefs[j].mResolutionProduct = 0x7FFFFFFF;
554 } else { // high resolution
555 mRequiredProfileRefs[i].mRefs[j].mResolutionProduct = 0;
556 }
557 }
558 }
559}
560
561int MediaProfiles::getRequiredProfileRefIndex(int cameraId) {
562 for (size_t i = 0, n = mCameraIds.size(); i < n; ++i) {
563 if (mCameraIds[i] == cameraId) {
564 return i;
565 }
566 }
567 return -1;
568}
569
570void MediaProfiles::checkAndAddRequiredProfilesIfNecessary() {
571 if (sIsInitialized) {
572 return;
573 }
574
575 initRequiredProfileRefs(mCameraIds);
576
577 for (size_t i = 0, n = mCamcorderProfiles.size(); i < n; ++i) {
578 int product = mCamcorderProfiles[i]->mVideoCodec->mFrameWidth *
579 mCamcorderProfiles[i]->mVideoCodec->mFrameHeight;
580
581 camcorder_quality quality = mCamcorderProfiles[i]->mQuality;
582 int cameraId = mCamcorderProfiles[i]->mCameraId;
583 int index = -1;
584 int refIndex = getRequiredProfileRefIndex(cameraId);
585 CHECK(refIndex != -1);
586 RequiredProfileRefInfo *info;
587 camcorder_quality refQuality;
James Dong2a7e0a12011-02-28 21:07:39 -0800588
Zhijun Hee0790972014-07-23 15:17:26 -0700589 // Check high and low from either camcorder profile, timelapse profile
590 // or high speed profile, but not all of them. Default, check camcorder profile
James Dong2a7e0a12011-02-28 21:07:39 -0800591 size_t j = 0;
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200592 size_t o = 2;
James Dong2a7e0a12011-02-28 21:07:39 -0800593 if (isTimelapseProfile(quality)) {
594 // Check timelapse profile instead.
595 j = 2;
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200596 o = kNumRequiredProfiles;
Zhijun Hee0790972014-07-23 15:17:26 -0700597 } else if (isHighSpeedProfile(quality)) {
598 // Skip the check for high speed profile.
599 continue;
James Dong2a7e0a12011-02-28 21:07:39 -0800600 } else {
601 // Must be camcorder profile.
602 CHECK(isCamcorderProfile(quality));
603 }
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200604 for (; j < o; ++j) {
James Dong2a7e0a12011-02-28 21:07:39 -0800605 info = &(mRequiredProfileRefs[refIndex].mRefs[j]);
606 if ((j % 2 == 0 && product > info->mResolutionProduct) || // low
607 (j % 2 != 0 && product < info->mResolutionProduct)) { // high
608 continue;
609 }
610 switch (j) {
611 case 0:
612 refQuality = CAMCORDER_QUALITY_LOW;
613 break;
614 case 1:
615 refQuality = CAMCORDER_QUALITY_HIGH;
616 break;
617 case 2:
618 refQuality = CAMCORDER_QUALITY_TIME_LAPSE_LOW;
619 break;
620 case 3:
621 refQuality = CAMCORDER_QUALITY_TIME_LAPSE_HIGH;
622 break;
623 default:
624 CHECK(!"Should never reach here");
625 }
626
627 if (!info->mHasRefProfile) {
628 index = getCamcorderProfileIndex(cameraId, refQuality);
629 }
630 if (index == -1) {
631 // New high or low quality profile is found.
632 // Update its reference.
633 info->mHasRefProfile = true;
634 info->mRefProfileIndex = i;
635 info->mResolutionProduct = product;
636 }
637 }
638 }
639
640 for (size_t cameraId = 0; cameraId < mCameraIds.size(); ++cameraId) {
641 for (size_t j = 0; j < kNumRequiredProfiles; ++j) {
642 int refIndex = getRequiredProfileRefIndex(cameraId);
643 CHECK(refIndex != -1);
644 RequiredProfileRefInfo *info =
645 &mRequiredProfileRefs[refIndex].mRefs[j];
646
647 if (info->mHasRefProfile) {
648
George Burgess IV215545b2018-07-25 10:06:30 -0700649 std::unique_ptr<CamcorderProfile> profile =
650 std::make_unique<CamcorderProfile>(
James Dong2a7e0a12011-02-28 21:07:39 -0800651 *mCamcorderProfiles[info->mRefProfileIndex]);
652
653 // Overwrite the quality
654 switch (j % kNumRequiredProfiles) {
655 case 0:
656 profile->mQuality = CAMCORDER_QUALITY_LOW;
657 break;
658 case 1:
659 profile->mQuality = CAMCORDER_QUALITY_HIGH;
660 break;
661 case 2:
662 profile->mQuality = CAMCORDER_QUALITY_TIME_LAPSE_LOW;
663 break;
664 case 3:
665 profile->mQuality = CAMCORDER_QUALITY_TIME_LAPSE_HIGH;
666 break;
667 default:
668 CHECK(!"Should never come here");
669 }
670
671 int index = getCamcorderProfileIndex(cameraId, profile->mQuality);
672 if (index != -1) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700673 ALOGV("Profile quality %d for camera %zu already exists",
James Dong2a7e0a12011-02-28 21:07:39 -0800674 profile->mQuality, cameraId);
675 CHECK(index == refIndex);
676 continue;
677 }
678
679 // Insert the new profile
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700680 ALOGV("Add a profile: quality %d=>%d for camera %zu",
James Dong2a7e0a12011-02-28 21:07:39 -0800681 mCamcorderProfiles[info->mRefProfileIndex]->mQuality,
682 profile->mQuality, cameraId);
683
George Burgess IV215545b2018-07-25 10:06:30 -0700684 mCamcorderProfiles.add(profile.release());
James Dong2a7e0a12011-02-28 21:07:39 -0800685 }
686 }
687 }
688}
689
James Dong1d7491b2010-01-19 17:45:38 -0800690/*static*/ MediaProfiles*
691MediaProfiles::getInstance()
692{
Steve Block3856b092011-10-20 11:56:00 +0100693 ALOGV("getInstance");
James Dong1d7491b2010-01-19 17:45:38 -0800694 Mutex::Autolock lock(sLock);
695 if (!sIsInitialized) {
696 char value[PROPERTY_VALUE_MAX];
697 if (property_get("media.settings.xml", value, NULL) <= 0) {
Pawin Vongmasad7db05b2017-05-03 04:19:20 -0700698 const char* xmlFile = nullptr;
Pawin Vongmasa7f5e10e2020-03-07 04:09:55 -0800699 for (auto const& f : getXmlPaths()) {
Pawin Vongmasad7db05b2017-05-03 04:19:20 -0700700 if (checkXmlFile(f)) {
701 xmlFile = f;
702 break;
703 }
704 }
705 if (xmlFile == nullptr) {
706 ALOGW("Could not find a validated xml file. "
707 "Using the default instance instead.");
James Dong1d7491b2010-01-19 17:45:38 -0800708 sInstance = createDefaultInstance();
709 } else {
Pawin Vongmasad7db05b2017-05-03 04:19:20 -0700710 sInstance = createInstanceFromXmlFile(xmlFile);
James Dong1d7491b2010-01-19 17:45:38 -0800711 }
712 } else {
713 sInstance = createInstanceFromXmlFile(value);
714 }
James Dong2a7e0a12011-02-28 21:07:39 -0800715 CHECK(sInstance != NULL);
716 sInstance->checkAndAddRequiredProfilesIfNecessary();
717 sIsInitialized = true;
James Dong1d7491b2010-01-19 17:45:38 -0800718 }
719
720 return sInstance;
721}
722
723/*static*/ MediaProfiles::VideoEncoderCap*
724MediaProfiles::createDefaultH263VideoEncoderCap()
725{
726 return new MediaProfiles::VideoEncoderCap(
727 VIDEO_ENCODER_H263, 192000, 420000, 176, 352, 144, 288, 1, 20);
728}
729
730/*static*/ MediaProfiles::VideoEncoderCap*
731MediaProfiles::createDefaultM4vVideoEncoderCap()
732{
733 return new MediaProfiles::VideoEncoderCap(
734 VIDEO_ENCODER_MPEG_4_SP, 192000, 420000, 176, 352, 144, 288, 1, 20);
735}
736
737
738/*static*/ void
739MediaProfiles::createDefaultVideoEncoders(MediaProfiles *profiles)
740{
741 profiles->mVideoEncoders.add(createDefaultH263VideoEncoderCap());
742 profiles->mVideoEncoders.add(createDefaultM4vVideoEncoderCap());
743}
744
745/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700746MediaProfiles::createDefaultCamcorderTimeLapseQcifProfile(camcorder_quality quality)
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700747{
748 MediaProfiles::VideoCodec *videoCodec =
749 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 1000000, 176, 144, 20);
750
751 AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
752 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
753 profile->mCameraId = 0;
754 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700755 profile->mQuality = quality;
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700756 profile->mDuration = 60;
757 profile->mVideoCodec = videoCodec;
758 profile->mAudioCodec = audioCodec;
759 return profile;
760}
761
762/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700763MediaProfiles::createDefaultCamcorderTimeLapse480pProfile(camcorder_quality quality)
James Dong1d7491b2010-01-19 17:45:38 -0800764{
765 MediaProfiles::VideoCodec *videoCodec =
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700766 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 20000000, 720, 480, 20);
James Dong1d7491b2010-01-19 17:45:38 -0800767
768 AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800769 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
770 profile->mCameraId = 0;
James Dong1d7491b2010-01-19 17:45:38 -0800771 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700772 profile->mQuality = quality;
James Dong1d7491b2010-01-19 17:45:38 -0800773 profile->mDuration = 60;
774 profile->mVideoCodec = videoCodec;
775 profile->mAudioCodec = audioCodec;
776 return profile;
777}
778
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700779/*static*/ void
780MediaProfiles::createDefaultCamcorderTimeLapseLowProfiles(
781 MediaProfiles::CamcorderProfile **lowTimeLapseProfile,
782 MediaProfiles::CamcorderProfile **lowSpecificTimeLapseProfile) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800783 *lowTimeLapseProfile = createDefaultCamcorderTimeLapseQcifProfile(
784 CAMCORDER_QUALITY_TIME_LAPSE_LOW);
785 *lowSpecificTimeLapseProfile = createDefaultCamcorderTimeLapseQcifProfile(
786 CAMCORDER_QUALITY_TIME_LAPSE_QCIF);
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700787}
788
789/*static*/ void
790MediaProfiles::createDefaultCamcorderTimeLapseHighProfiles(
791 MediaProfiles::CamcorderProfile **highTimeLapseProfile,
792 MediaProfiles::CamcorderProfile **highSpecificTimeLapseProfile) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800793 *highTimeLapseProfile = createDefaultCamcorderTimeLapse480pProfile(
794 CAMCORDER_QUALITY_TIME_LAPSE_HIGH);
795 *highSpecificTimeLapseProfile = createDefaultCamcorderTimeLapse480pProfile(
796 CAMCORDER_QUALITY_TIME_LAPSE_480P);
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700797}
798
James Dong1d7491b2010-01-19 17:45:38 -0800799/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700800MediaProfiles::createDefaultCamcorderQcifProfile(camcorder_quality quality)
James Dong1d7491b2010-01-19 17:45:38 -0800801{
802 MediaProfiles::VideoCodec *videoCodec =
803 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 192000, 176, 144, 20);
804
805 MediaProfiles::AudioCodec *audioCodec =
806 new MediaProfiles::AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
807
808 MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800809 profile->mCameraId = 0;
James Dong1d7491b2010-01-19 17:45:38 -0800810 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700811 profile->mQuality = quality;
James Dong1d7491b2010-01-19 17:45:38 -0800812 profile->mDuration = 30;
813 profile->mVideoCodec = videoCodec;
814 profile->mAudioCodec = audioCodec;
815 return profile;
816}
817
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700818/*static*/ MediaProfiles::CamcorderProfile*
819MediaProfiles::createDefaultCamcorderCifProfile(camcorder_quality quality)
820{
821 MediaProfiles::VideoCodec *videoCodec =
822 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 360000, 352, 288, 20);
823
824 AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
825 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
826 profile->mCameraId = 0;
827 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
828 profile->mQuality = quality;
829 profile->mDuration = 60;
830 profile->mVideoCodec = videoCodec;
831 profile->mAudioCodec = audioCodec;
832 return profile;
833}
834
835/*static*/ void
836MediaProfiles::createDefaultCamcorderLowProfiles(
837 MediaProfiles::CamcorderProfile **lowProfile,
838 MediaProfiles::CamcorderProfile **lowSpecificProfile) {
839 *lowProfile = createDefaultCamcorderQcifProfile(CAMCORDER_QUALITY_LOW);
840 *lowSpecificProfile = createDefaultCamcorderQcifProfile(CAMCORDER_QUALITY_QCIF);
841}
842
843/*static*/ void
844MediaProfiles::createDefaultCamcorderHighProfiles(
845 MediaProfiles::CamcorderProfile **highProfile,
846 MediaProfiles::CamcorderProfile **highSpecificProfile) {
847 *highProfile = createDefaultCamcorderCifProfile(CAMCORDER_QUALITY_HIGH);
848 *highSpecificProfile = createDefaultCamcorderCifProfile(CAMCORDER_QUALITY_CIF);
849}
850
James Dong1d7491b2010-01-19 17:45:38 -0800851/*static*/ void
852MediaProfiles::createDefaultCamcorderProfiles(MediaProfiles *profiles)
853{
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700854 // low camcorder profiles.
855 MediaProfiles::CamcorderProfile *lowProfile, *lowSpecificProfile;
856 createDefaultCamcorderLowProfiles(&lowProfile, &lowSpecificProfile);
857 profiles->mCamcorderProfiles.add(lowProfile);
858 profiles->mCamcorderProfiles.add(lowSpecificProfile);
859
860 // high camcorder profiles.
861 MediaProfiles::CamcorderProfile* highProfile, *highSpecificProfile;
862 createDefaultCamcorderHighProfiles(&highProfile, &highSpecificProfile);
863 profiles->mCamcorderProfiles.add(highProfile);
864 profiles->mCamcorderProfiles.add(highSpecificProfile);
865
866 // low camcorder time lapse profiles.
867 MediaProfiles::CamcorderProfile *lowTimeLapseProfile, *lowSpecificTimeLapseProfile;
868 createDefaultCamcorderTimeLapseLowProfiles(&lowTimeLapseProfile, &lowSpecificTimeLapseProfile);
869 profiles->mCamcorderProfiles.add(lowTimeLapseProfile);
870 profiles->mCamcorderProfiles.add(lowSpecificTimeLapseProfile);
871
872 // high camcorder time lapse profiles.
873 MediaProfiles::CamcorderProfile *highTimeLapseProfile, *highSpecificTimeLapseProfile;
Glenn Kastenb187de12014-12-30 08:18:15 -0800874 createDefaultCamcorderTimeLapseHighProfiles(&highTimeLapseProfile,
875 &highSpecificTimeLapseProfile);
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700876 profiles->mCamcorderProfiles.add(highTimeLapseProfile);
877 profiles->mCamcorderProfiles.add(highSpecificTimeLapseProfile);
James Dong8031ec72011-03-16 14:09:50 -0700878
879 // For emulator and other legacy devices which does not have a
880 // media_profiles.xml file, We assume that the default camera id
881 // is 0 and that is the only camera available.
882 profiles->mCameraIds.push(0);
James Dong1d7491b2010-01-19 17:45:38 -0800883}
884
885/*static*/ void
886MediaProfiles::createDefaultAudioEncoders(MediaProfiles *profiles)
887{
888 profiles->mAudioEncoders.add(createDefaultAmrNBEncoderCap());
889}
890
891/*static*/ void
892MediaProfiles::createDefaultVideoDecoders(MediaProfiles *profiles)
893{
894 MediaProfiles::VideoDecoderCap *cap =
895 new MediaProfiles::VideoDecoderCap(VIDEO_DECODER_WMV);
896
897 profiles->mVideoDecoders.add(cap);
898}
899
900/*static*/ void
901MediaProfiles::createDefaultAudioDecoders(MediaProfiles *profiles)
902{
903 MediaProfiles::AudioDecoderCap *cap =
904 new MediaProfiles::AudioDecoderCap(AUDIO_DECODER_WMA);
905
906 profiles->mAudioDecoders.add(cap);
907}
908
909/*static*/ void
910MediaProfiles::createDefaultEncoderOutputFileFormats(MediaProfiles *profiles)
911{
912 profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_THREE_GPP);
913 profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_MPEG_4);
914}
915
916/*static*/ MediaProfiles::AudioEncoderCap*
917MediaProfiles::createDefaultAmrNBEncoderCap()
918{
919 return new MediaProfiles::AudioEncoderCap(
920 AUDIO_ENCODER_AMR_NB, 5525, 12200, 8000, 8000, 1, 1);
921}
922
James Dongf5a83852010-02-23 17:21:44 -0800923/*static*/ void
924MediaProfiles::createDefaultImageEncodingQualityLevels(MediaProfiles *profiles)
925{
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800926 ImageEncodingQualityLevels *levels = new ImageEncodingQualityLevels();
927 levels->mCameraId = 0;
928 levels->mLevels.add(70);
929 levels->mLevels.add(80);
930 levels->mLevels.add(90);
931 profiles->mImageEncodingQualityLevels.add(levels);
James Dongf5a83852010-02-23 17:21:44 -0800932}
933
James Dong1d7491b2010-01-19 17:45:38 -0800934/*static*/ MediaProfiles*
935MediaProfiles::createDefaultInstance()
936{
937 MediaProfiles *profiles = new MediaProfiles;
938 createDefaultCamcorderProfiles(profiles);
939 createDefaultVideoEncoders(profiles);
940 createDefaultAudioEncoders(profiles);
941 createDefaultVideoDecoders(profiles);
942 createDefaultAudioDecoders(profiles);
943 createDefaultEncoderOutputFileFormats(profiles);
James Dongf5a83852010-02-23 17:21:44 -0800944 createDefaultImageEncodingQualityLevels(profiles);
James Dong1d7491b2010-01-19 17:45:38 -0800945 return profiles;
946}
947
Pawin Vongmasad7db05b2017-05-03 04:19:20 -0700948bool MediaProfiles::checkXmlFile(const char* xmlFile) {
949 struct stat fStat;
950 return stat(xmlFile, &fStat) == 0 && S_ISREG(fStat.st_mode);
951 // TODO: Add validation
952}
953
James Dong1d7491b2010-01-19 17:45:38 -0800954/*static*/ MediaProfiles*
955MediaProfiles::createInstanceFromXmlFile(const char *xml)
956{
957 FILE *fp = NULL;
958 CHECK((fp = fopen(xml, "r")));
959
960 XML_Parser parser = ::XML_ParserCreate(NULL);
961 CHECK(parser != NULL);
962
963 MediaProfiles *profiles = new MediaProfiles();
964 ::XML_SetUserData(parser, profiles);
965 ::XML_SetElementHandler(parser, startElementHandler, NULL);
966
967 /*
968 FIXME:
969 expat is not compiled with -DXML_DTD. We don't have DTD parsing support.
970
971 if (!::XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS)) {
Steve Block29357bc2012-01-06 19:20:56 +0000972 ALOGE("failed to enable DTD support in the xml file");
James Dong1d7491b2010-01-19 17:45:38 -0800973 return UNKNOWN_ERROR;
974 }
975
976 */
977
978 const int BUFF_SIZE = 512;
979 for (;;) {
980 void *buff = ::XML_GetBuffer(parser, BUFF_SIZE);
981 if (buff == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000982 ALOGE("failed to in call to XML_GetBuffer()");
James Dong1d7491b2010-01-19 17:45:38 -0800983 delete profiles;
984 profiles = NULL;
985 goto exit;
986 }
987
988 int bytes_read = ::fread(buff, 1, BUFF_SIZE, fp);
989 if (bytes_read < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000990 ALOGE("failed in call to read");
James Dong1d7491b2010-01-19 17:45:38 -0800991 delete profiles;
992 profiles = NULL;
993 goto exit;
994 }
995
996 CHECK(::XML_ParseBuffer(parser, bytes_read, bytes_read == 0));
997
998 if (bytes_read == 0) break; // done parsing the xml file
999 }
1000
1001exit:
1002 ::XML_ParserFree(parser);
1003 ::fclose(fp);
James Dong1d7491b2010-01-19 17:45:38 -08001004 return profiles;
1005}
1006
1007Vector<output_format> MediaProfiles::getOutputFileFormats() const
1008{
1009 return mEncoderOutputFileFormats; // copy out
1010}
1011
1012Vector<video_encoder> MediaProfiles::getVideoEncoders() const
1013{
1014 Vector<video_encoder> encoders;
1015 for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
1016 encoders.add(mVideoEncoders[i]->mCodec);
1017 }
1018 return encoders; // copy out
1019}
1020
1021int MediaProfiles::getVideoEncoderParamByName(const char *name, video_encoder codec) const
1022{
Steve Block3856b092011-10-20 11:56:00 +01001023 ALOGV("getVideoEncoderParamByName: %s for codec %d", name, codec);
James Dong1d7491b2010-01-19 17:45:38 -08001024 int index = -1;
1025 for (size_t i = 0, n = mVideoEncoders.size(); i < n; ++i) {
1026 if (mVideoEncoders[i]->mCodec == codec) {
1027 index = i;
1028 break;
1029 }
1030 }
1031 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +00001032 ALOGE("The given video encoder %d is not found", codec);
James Dong1d7491b2010-01-19 17:45:38 -08001033 return -1;
1034 }
1035
1036 if (!strcmp("enc.vid.width.min", name)) return mVideoEncoders[index]->mMinFrameWidth;
1037 if (!strcmp("enc.vid.width.max", name)) return mVideoEncoders[index]->mMaxFrameWidth;
1038 if (!strcmp("enc.vid.height.min", name)) return mVideoEncoders[index]->mMinFrameHeight;
1039 if (!strcmp("enc.vid.height.max", name)) return mVideoEncoders[index]->mMaxFrameHeight;
1040 if (!strcmp("enc.vid.bps.min", name)) return mVideoEncoders[index]->mMinBitRate;
1041 if (!strcmp("enc.vid.bps.max", name)) return mVideoEncoders[index]->mMaxBitRate;
1042 if (!strcmp("enc.vid.fps.min", name)) return mVideoEncoders[index]->mMinFrameRate;
1043 if (!strcmp("enc.vid.fps.max", name)) return mVideoEncoders[index]->mMaxFrameRate;
1044
Steve Block29357bc2012-01-06 19:20:56 +00001045 ALOGE("The given video encoder param name %s is not found", name);
James Dong1d7491b2010-01-19 17:45:38 -08001046 return -1;
1047}
Hong Tengcabd5f82011-07-06 18:33:09 -07001048
James Dong1d7491b2010-01-19 17:45:38 -08001049Vector<audio_encoder> MediaProfiles::getAudioEncoders() const
1050{
1051 Vector<audio_encoder> encoders;
1052 for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
1053 encoders.add(mAudioEncoders[i]->mCodec);
1054 }
1055 return encoders; // copy out
1056}
1057
1058int MediaProfiles::getAudioEncoderParamByName(const char *name, audio_encoder codec) const
1059{
Steve Block3856b092011-10-20 11:56:00 +01001060 ALOGV("getAudioEncoderParamByName: %s for codec %d", name, codec);
James Dong1d7491b2010-01-19 17:45:38 -08001061 int index = -1;
1062 for (size_t i = 0, n = mAudioEncoders.size(); i < n; ++i) {
1063 if (mAudioEncoders[i]->mCodec == codec) {
1064 index = i;
1065 break;
1066 }
1067 }
1068 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +00001069 ALOGE("The given audio encoder %d is not found", codec);
James Dong1d7491b2010-01-19 17:45:38 -08001070 return -1;
1071 }
1072
1073 if (!strcmp("enc.aud.ch.min", name)) return mAudioEncoders[index]->mMinChannels;
1074 if (!strcmp("enc.aud.ch.max", name)) return mAudioEncoders[index]->mMaxChannels;
1075 if (!strcmp("enc.aud.bps.min", name)) return mAudioEncoders[index]->mMinBitRate;
1076 if (!strcmp("enc.aud.bps.max", name)) return mAudioEncoders[index]->mMaxBitRate;
1077 if (!strcmp("enc.aud.hz.min", name)) return mAudioEncoders[index]->mMinSampleRate;
1078 if (!strcmp("enc.aud.hz.max", name)) return mAudioEncoders[index]->mMaxSampleRate;
1079
Steve Block29357bc2012-01-06 19:20:56 +00001080 ALOGE("The given audio encoder param name %s is not found", name);
James Dong1d7491b2010-01-19 17:45:38 -08001081 return -1;
1082}
1083
1084Vector<video_decoder> MediaProfiles::getVideoDecoders() const
1085{
1086 Vector<video_decoder> decoders;
1087 for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
1088 decoders.add(mVideoDecoders[i]->mCodec);
1089 }
1090 return decoders; // copy out
1091}
1092
1093Vector<audio_decoder> MediaProfiles::getAudioDecoders() const
1094{
1095 Vector<audio_decoder> decoders;
1096 for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
1097 decoders.add(mAudioDecoders[i]->mCodec);
1098 }
1099 return decoders; // copy out
1100}
1101
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001102int MediaProfiles::getCamcorderProfileIndex(int cameraId, camcorder_quality quality) const
James Dong1d7491b2010-01-19 17:45:38 -08001103{
James Dong1d7491b2010-01-19 17:45:38 -08001104 int index = -1;
1105 for (size_t i = 0, n = mCamcorderProfiles.size(); i < n; ++i) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001106 if (mCamcorderProfiles[i]->mCameraId == cameraId &&
1107 mCamcorderProfiles[i]->mQuality == quality) {
James Dong1d7491b2010-01-19 17:45:38 -08001108 index = i;
1109 break;
1110 }
1111 }
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001112 return index;
1113}
1114
1115int MediaProfiles::getCamcorderProfileParamByName(const char *name,
1116 int cameraId,
1117 camcorder_quality quality) const
1118{
Steve Block3856b092011-10-20 11:56:00 +01001119 ALOGV("getCamcorderProfileParamByName: %s for camera %d, quality %d",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001120 name, cameraId, quality);
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001121
1122 int index = getCamcorderProfileIndex(cameraId, quality);
James Dong1d7491b2010-01-19 17:45:38 -08001123 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +00001124 ALOGE("The given camcorder profile camera %d quality %d is not found",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001125 cameraId, quality);
James Dong1d7491b2010-01-19 17:45:38 -08001126 return -1;
1127 }
1128
James Dongf5a83852010-02-23 17:21:44 -08001129 if (!strcmp("duration", name)) return mCamcorderProfiles[index]->mDuration;
James Dong1d7491b2010-01-19 17:45:38 -08001130 if (!strcmp("file.format", name)) return mCamcorderProfiles[index]->mFileFormat;
1131 if (!strcmp("vid.codec", name)) return mCamcorderProfiles[index]->mVideoCodec->mCodec;
1132 if (!strcmp("vid.width", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameWidth;
1133 if (!strcmp("vid.height", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameHeight;
1134 if (!strcmp("vid.bps", name)) return mCamcorderProfiles[index]->mVideoCodec->mBitRate;
1135 if (!strcmp("vid.fps", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameRate;
1136 if (!strcmp("aud.codec", name)) return mCamcorderProfiles[index]->mAudioCodec->mCodec;
1137 if (!strcmp("aud.bps", name)) return mCamcorderProfiles[index]->mAudioCodec->mBitRate;
1138 if (!strcmp("aud.ch", name)) return mCamcorderProfiles[index]->mAudioCodec->mChannels;
1139 if (!strcmp("aud.hz", name)) return mCamcorderProfiles[index]->mAudioCodec->mSampleRate;
1140
Steve Block29357bc2012-01-06 19:20:56 +00001141 ALOGE("The given camcorder profile param id %d name %s is not found", cameraId, name);
James Dong1d7491b2010-01-19 17:45:38 -08001142 return -1;
1143}
1144
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001145bool MediaProfiles::hasCamcorderProfile(int cameraId, camcorder_quality quality) const
1146{
1147 return (getCamcorderProfileIndex(cameraId, quality) != -1);
1148}
1149
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001150Vector<int> MediaProfiles::getImageEncodingQualityLevels(int cameraId) const
James Dongf5a83852010-02-23 17:21:44 -08001151{
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001152 Vector<int> result;
1153 ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
1154 if (levels != NULL) {
1155 result = levels->mLevels; // copy out
1156 }
1157 return result;
James Dongf5a83852010-02-23 17:21:44 -08001158}
1159
James Dong0f056292011-05-09 18:49:31 -07001160int MediaProfiles::getStartTimeOffsetMs(int cameraId) const {
1161 int offsetTimeMs = -1;
1162 ssize_t index = mStartTimeOffsets.indexOfKey(cameraId);
1163 if (index >= 0) {
1164 offsetTimeMs = mStartTimeOffsets.valueFor(cameraId);
1165 }
Steve Block3856b092011-10-20 11:56:00 +01001166 ALOGV("offsetTime=%d ms and cameraId=%d", offsetTimeMs, cameraId);
James Dong0f056292011-05-09 18:49:31 -07001167 return offsetTimeMs;
1168}
1169
James Dong1d7491b2010-01-19 17:45:38 -08001170MediaProfiles::~MediaProfiles()
1171{
1172 CHECK("destructor should never be called" == 0);
1173#if 0
1174 for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
1175 delete mAudioEncoders[i];
1176 }
1177 mAudioEncoders.clear();
1178
1179 for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
1180 delete mVideoEncoders[i];
1181 }
1182 mVideoEncoders.clear();
1183
1184 for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
1185 delete mVideoDecoders[i];
1186 }
1187 mVideoDecoders.clear();
1188
1189 for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
1190 delete mAudioDecoders[i];
1191 }
1192 mAudioDecoders.clear();
1193
1194 for (size_t i = 0; i < mCamcorderProfiles.size(); ++i) {
1195 delete mCamcorderProfiles[i];
1196 }
1197 mCamcorderProfiles.clear();
1198#endif
1199}
1200} // namespace android