blob: aade69a0939770e0caea01392c7f7916d9ef7d0c [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
32namespace android {
33
Pawin Vongmasad7db05b2017-05-03 04:19:20 -070034constexpr char const * const MediaProfiles::xmlFiles[];
James Dong1d7491b2010-01-19 17:45:38 -080035Mutex MediaProfiles::sLock;
36bool MediaProfiles::sIsInitialized = false;
37MediaProfiles *MediaProfiles::sInstance = NULL;
38
39const MediaProfiles::NameToTagMap MediaProfiles::sVideoEncoderNameMap[] = {
40 {"h263", VIDEO_ENCODER_H263},
41 {"h264", VIDEO_ENCODER_H264},
Wonsik Kim9aa87d42015-12-07 13:52:02 +090042 {"m4v", VIDEO_ENCODER_MPEG_4_SP},
43 {"hevc", VIDEO_ENCODER_HEVC}
James Dong1d7491b2010-01-19 17:45:38 -080044};
45
46const MediaProfiles::NameToTagMap MediaProfiles::sAudioEncoderNameMap[] = {
Dave Burkeaeb8fd42012-04-19 00:14:27 -070047 {"amrnb", AUDIO_ENCODER_AMR_NB},
48 {"amrwb", AUDIO_ENCODER_AMR_WB},
49 {"aac", AUDIO_ENCODER_AAC},
Dave Burkef60c6602012-04-28 21:58:22 -070050 {"heaac", AUDIO_ENCODER_HE_AAC},
51 {"aaceld", AUDIO_ENCODER_AAC_ELD}
James Dong1d7491b2010-01-19 17:45:38 -080052};
53
54const MediaProfiles::NameToTagMap MediaProfiles::sFileFormatMap[] = {
55 {"3gp", OUTPUT_FORMAT_THREE_GPP},
56 {"mp4", OUTPUT_FORMAT_MPEG_4}
57};
58
59const MediaProfiles::NameToTagMap MediaProfiles::sVideoDecoderNameMap[] = {
60 {"wmv", VIDEO_DECODER_WMV}
61};
62
63const MediaProfiles::NameToTagMap MediaProfiles::sAudioDecoderNameMap[] = {
64 {"wma", AUDIO_DECODER_WMA}
65};
66
67const MediaProfiles::NameToTagMap MediaProfiles::sCamcorderQualityNameMap[] = {
Nipun Kwatrac0a84782010-09-06 15:59:02 -070068 {"low", CAMCORDER_QUALITY_LOW},
James Dong1d7491b2010-01-19 17:45:38 -080069 {"high", CAMCORDER_QUALITY_HIGH},
Nipun Kwatrac0a84782010-09-06 15:59:02 -070070 {"qcif", CAMCORDER_QUALITY_QCIF},
Nipun Kwatra9783ed82010-09-10 15:45:57 -070071 {"cif", CAMCORDER_QUALITY_CIF},
Nipun Kwatrac0a84782010-09-06 15:59:02 -070072 {"480p", CAMCORDER_QUALITY_480P},
73 {"720p", CAMCORDER_QUALITY_720P},
74 {"1080p", CAMCORDER_QUALITY_1080P},
Zhijun He5f6af1a2014-06-10 08:26:33 -070075 {"2160p", CAMCORDER_QUALITY_2160P},
James Dong669012d2011-09-19 16:27:31 -070076 {"qvga", CAMCORDER_QUALITY_QVGA},
Nipun Kwatrac0a84782010-09-06 15:59:02 -070077
78 {"timelapselow", CAMCORDER_QUALITY_TIME_LAPSE_LOW},
79 {"timelapsehigh", CAMCORDER_QUALITY_TIME_LAPSE_HIGH},
80 {"timelapseqcif", CAMCORDER_QUALITY_TIME_LAPSE_QCIF},
Nipun Kwatra9783ed82010-09-10 15:45:57 -070081 {"timelapsecif", CAMCORDER_QUALITY_TIME_LAPSE_CIF},
Nipun Kwatrac0a84782010-09-06 15:59:02 -070082 {"timelapse480p", CAMCORDER_QUALITY_TIME_LAPSE_480P},
83 {"timelapse720p", CAMCORDER_QUALITY_TIME_LAPSE_720P},
James Dong669012d2011-09-19 16:27:31 -070084 {"timelapse1080p", CAMCORDER_QUALITY_TIME_LAPSE_1080P},
Zhijun He5f6af1a2014-06-10 08:26:33 -070085 {"timelapse2160p", CAMCORDER_QUALITY_TIME_LAPSE_2160P},
James Dong669012d2011-09-19 16:27:31 -070086 {"timelapseqvga", CAMCORDER_QUALITY_TIME_LAPSE_QVGA},
Zhijun Hee0790972014-07-23 15:17:26 -070087
88 {"highspeedlow", CAMCORDER_QUALITY_HIGH_SPEED_LOW},
89 {"highspeedhigh", CAMCORDER_QUALITY_HIGH_SPEED_HIGH},
90 {"highspeed480p", CAMCORDER_QUALITY_HIGH_SPEED_480P},
91 {"highspeed720p", CAMCORDER_QUALITY_HIGH_SPEED_720P},
92 {"highspeed1080p", CAMCORDER_QUALITY_HIGH_SPEED_1080P},
Zhijun He9520aa62014-09-09 16:18:31 -070093 {"highspeed2160p", CAMCORDER_QUALITY_HIGH_SPEED_2160P},
James Dong1d7491b2010-01-19 17:45:38 -080094};
95
Glenn Kasten80520382014-01-31 16:49:31 -080096#if LOG_NDEBUG
97#define UNUSED __unused
98#else
99#define UNUSED
100#endif
101
James Dong1d7491b2010-01-19 17:45:38 -0800102/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800103MediaProfiles::logVideoCodec(const MediaProfiles::VideoCodec& codec UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800104{
Steve Block3856b092011-10-20 11:56:00 +0100105 ALOGV("video codec:");
106 ALOGV("codec = %d", codec.mCodec);
107 ALOGV("bit rate: %d", codec.mBitRate);
108 ALOGV("frame width: %d", codec.mFrameWidth);
109 ALOGV("frame height: %d", codec.mFrameHeight);
110 ALOGV("frame rate: %d", codec.mFrameRate);
James Dong1d7491b2010-01-19 17:45:38 -0800111}
112
113/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800114MediaProfiles::logAudioCodec(const MediaProfiles::AudioCodec& codec UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800115{
Steve Block3856b092011-10-20 11:56:00 +0100116 ALOGV("audio codec:");
117 ALOGV("codec = %d", codec.mCodec);
118 ALOGV("bit rate: %d", codec.mBitRate);
119 ALOGV("sample rate: %d", codec.mSampleRate);
120 ALOGV("number of channels: %d", codec.mChannels);
James Dong1d7491b2010-01-19 17:45:38 -0800121}
122
123/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800124MediaProfiles::logVideoEncoderCap(const MediaProfiles::VideoEncoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800125{
Steve Block3856b092011-10-20 11:56:00 +0100126 ALOGV("video encoder cap:");
127 ALOGV("codec = %d", cap.mCodec);
128 ALOGV("bit rate: min = %d and max = %d", cap.mMinBitRate, cap.mMaxBitRate);
129 ALOGV("frame width: min = %d and max = %d", cap.mMinFrameWidth, cap.mMaxFrameWidth);
130 ALOGV("frame height: min = %d and max = %d", cap.mMinFrameHeight, cap.mMaxFrameHeight);
131 ALOGV("frame rate: min = %d and max = %d", cap.mMinFrameRate, cap.mMaxFrameRate);
James Dong1d7491b2010-01-19 17:45:38 -0800132}
133
134/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800135MediaProfiles::logAudioEncoderCap(const MediaProfiles::AudioEncoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800136{
Steve Block3856b092011-10-20 11:56:00 +0100137 ALOGV("audio encoder cap:");
138 ALOGV("codec = %d", cap.mCodec);
139 ALOGV("bit rate: min = %d and max = %d", cap.mMinBitRate, cap.mMaxBitRate);
140 ALOGV("sample rate: min = %d and max = %d", cap.mMinSampleRate, cap.mMaxSampleRate);
141 ALOGV("number of channels: min = %d and max = %d", cap.mMinChannels, cap.mMaxChannels);
James Dong1d7491b2010-01-19 17:45:38 -0800142}
143
144/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800145MediaProfiles::logVideoDecoderCap(const MediaProfiles::VideoDecoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800146{
Steve Block3856b092011-10-20 11:56:00 +0100147 ALOGV("video decoder cap:");
148 ALOGV("codec = %d", cap.mCodec);
James Dong1d7491b2010-01-19 17:45:38 -0800149}
150
151/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800152MediaProfiles::logAudioDecoderCap(const MediaProfiles::AudioDecoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800153{
Steve Block3856b092011-10-20 11:56:00 +0100154 ALOGV("audio codec cap:");
155 ALOGV("codec = %d", cap.mCodec);
James Dong1d7491b2010-01-19 17:45:38 -0800156}
157
158/*static*/ int
Glenn Kastenb187de12014-12-30 08:18:15 -0800159MediaProfiles::findTagForName(const MediaProfiles::NameToTagMap *map, size_t nMappings,
160 const char *name)
James Dong1d7491b2010-01-19 17:45:38 -0800161{
162 int tag = -1;
163 for (size_t i = 0; i < nMappings; ++i) {
164 if (!strcmp(map[i].name, name)) {
165 tag = map[i].tag;
166 break;
167 }
168 }
169 return tag;
170}
171
172/*static*/ MediaProfiles::VideoCodec*
173MediaProfiles::createVideoCodec(const char **atts, MediaProfiles *profiles)
174{
175 CHECK(!strcmp("codec", atts[0]) &&
176 !strcmp("bitRate", atts[2]) &&
177 !strcmp("width", atts[4]) &&
178 !strcmp("height", atts[6]) &&
179 !strcmp("frameRate", atts[8]));
180
181 const size_t nMappings = sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
182 const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
183 CHECK(codec != -1);
184
185 MediaProfiles::VideoCodec *videoCodec =
186 new MediaProfiles::VideoCodec(static_cast<video_encoder>(codec),
187 atoi(atts[3]), atoi(atts[5]), atoi(atts[7]), atoi(atts[9]));
188 logVideoCodec(*videoCodec);
189
190 size_t nCamcorderProfiles;
191 CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
192 profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mVideoCodec = videoCodec;
193 return videoCodec;
194}
195
196/*static*/ MediaProfiles::AudioCodec*
197MediaProfiles::createAudioCodec(const char **atts, MediaProfiles *profiles)
198{
199 CHECK(!strcmp("codec", atts[0]) &&
200 !strcmp("bitRate", atts[2]) &&
201 !strcmp("sampleRate", atts[4]) &&
202 !strcmp("channels", atts[6]));
203 const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
204 const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
205 CHECK(codec != -1);
206
207 MediaProfiles::AudioCodec *audioCodec =
208 new MediaProfiles::AudioCodec(static_cast<audio_encoder>(codec),
209 atoi(atts[3]), atoi(atts[5]), atoi(atts[7]));
210 logAudioCodec(*audioCodec);
211
212 size_t nCamcorderProfiles;
213 CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
214 profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mAudioCodec = audioCodec;
215 return audioCodec;
216}
217/*static*/ MediaProfiles::AudioDecoderCap*
218MediaProfiles::createAudioDecoderCap(const char **atts)
219{
220 CHECK(!strcmp("name", atts[0]) &&
221 !strcmp("enabled", atts[2]));
222
223 const size_t nMappings = sizeof(sAudioDecoderNameMap)/sizeof(sAudioDecoderNameMap[0]);
224 const int codec = findTagForName(sAudioDecoderNameMap, nMappings, atts[1]);
225 CHECK(codec != -1);
226
227 MediaProfiles::AudioDecoderCap *cap =
228 new MediaProfiles::AudioDecoderCap(static_cast<audio_decoder>(codec));
229 logAudioDecoderCap(*cap);
230 return cap;
231}
232
233/*static*/ MediaProfiles::VideoDecoderCap*
234MediaProfiles::createVideoDecoderCap(const char **atts)
235{
236 CHECK(!strcmp("name", atts[0]) &&
237 !strcmp("enabled", atts[2]));
238
239 const size_t nMappings = sizeof(sVideoDecoderNameMap)/sizeof(sVideoDecoderNameMap[0]);
240 const int codec = findTagForName(sVideoDecoderNameMap, nMappings, atts[1]);
241 CHECK(codec != -1);
242
243 MediaProfiles::VideoDecoderCap *cap =
244 new MediaProfiles::VideoDecoderCap(static_cast<video_decoder>(codec));
245 logVideoDecoderCap(*cap);
246 return cap;
247}
248
249/*static*/ MediaProfiles::VideoEncoderCap*
250MediaProfiles::createVideoEncoderCap(const char **atts)
251{
252 CHECK(!strcmp("name", atts[0]) &&
253 !strcmp("enabled", atts[2]) &&
254 !strcmp("minBitRate", atts[4]) &&
255 !strcmp("maxBitRate", atts[6]) &&
256 !strcmp("minFrameWidth", atts[8]) &&
257 !strcmp("maxFrameWidth", atts[10]) &&
258 !strcmp("minFrameHeight", atts[12]) &&
259 !strcmp("maxFrameHeight", atts[14]) &&
260 !strcmp("minFrameRate", atts[16]) &&
261 !strcmp("maxFrameRate", atts[18]));
262
263 const size_t nMappings = sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
264 const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
265 CHECK(codec != -1);
266
267 MediaProfiles::VideoEncoderCap *cap =
268 new MediaProfiles::VideoEncoderCap(static_cast<video_encoder>(codec),
269 atoi(atts[5]), atoi(atts[7]), atoi(atts[9]), atoi(atts[11]), atoi(atts[13]),
270 atoi(atts[15]), atoi(atts[17]), atoi(atts[19]));
271 logVideoEncoderCap(*cap);
272 return cap;
273}
274
275/*static*/ MediaProfiles::AudioEncoderCap*
276MediaProfiles::createAudioEncoderCap(const char **atts)
277{
278 CHECK(!strcmp("name", atts[0]) &&
279 !strcmp("enabled", atts[2]) &&
280 !strcmp("minBitRate", atts[4]) &&
281 !strcmp("maxBitRate", atts[6]) &&
282 !strcmp("minSampleRate", atts[8]) &&
283 !strcmp("maxSampleRate", atts[10]) &&
284 !strcmp("minChannels", atts[12]) &&
285 !strcmp("maxChannels", atts[14]));
286
287 const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
288 const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
289 CHECK(codec != -1);
290
291 MediaProfiles::AudioEncoderCap *cap =
Glenn Kastenb187de12014-12-30 08:18:15 -0800292 new MediaProfiles::AudioEncoderCap(static_cast<audio_encoder>(codec), atoi(atts[5]),
293 atoi(atts[7]), atoi(atts[9]), atoi(atts[11]), atoi(atts[13]), atoi(atts[15]));
James Dong1d7491b2010-01-19 17:45:38 -0800294 logAudioEncoderCap(*cap);
295 return cap;
296}
297
298/*static*/ output_format
299MediaProfiles::createEncoderOutputFileFormat(const char **atts)
300{
301 CHECK(!strcmp("name", atts[0]));
302
303 const size_t nMappings =sizeof(sFileFormatMap)/sizeof(sFileFormatMap[0]);
304 const int format = findTagForName(sFileFormatMap, nMappings, atts[1]);
305 CHECK(format != -1);
306
307 return static_cast<output_format>(format);
308}
309
James Dong2a7e0a12011-02-28 21:07:39 -0800310static bool isCameraIdFound(int cameraId, const Vector<int>& cameraIds) {
311 for (int i = 0, n = cameraIds.size(); i < n; ++i) {
312 if (cameraId == cameraIds[i]) {
313 return true;
314 }
315 }
316 return false;
317}
318
James Dong1d7491b2010-01-19 17:45:38 -0800319/*static*/ MediaProfiles::CamcorderProfile*
James Dong2a7e0a12011-02-28 21:07:39 -0800320MediaProfiles::createCamcorderProfile(int cameraId, const char **atts, Vector<int>& cameraIds)
James Dong1d7491b2010-01-19 17:45:38 -0800321{
322 CHECK(!strcmp("quality", atts[0]) &&
323 !strcmp("fileFormat", atts[2]) &&
324 !strcmp("duration", atts[4]));
325
Glenn Kastenb187de12014-12-30 08:18:15 -0800326 const size_t nProfileMappings = sizeof(sCamcorderQualityNameMap)/
327 sizeof(sCamcorderQualityNameMap[0]);
James Dong1d7491b2010-01-19 17:45:38 -0800328 const int quality = findTagForName(sCamcorderQualityNameMap, nProfileMappings, atts[1]);
329 CHECK(quality != -1);
330
331 const size_t nFormatMappings = sizeof(sFileFormatMap)/sizeof(sFileFormatMap[0]);
332 const int fileFormat = findTagForName(sFileFormatMap, nFormatMappings, atts[3]);
333 CHECK(fileFormat != -1);
334
335 MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800336 profile->mCameraId = cameraId;
James Dong2a7e0a12011-02-28 21:07:39 -0800337 if (!isCameraIdFound(cameraId, cameraIds)) {
338 cameraIds.add(cameraId);
339 }
James Dong1d7491b2010-01-19 17:45:38 -0800340 profile->mFileFormat = static_cast<output_format>(fileFormat);
341 profile->mQuality = static_cast<camcorder_quality>(quality);
342 profile->mDuration = atoi(atts[5]);
343 return profile;
344}
345
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800346MediaProfiles::ImageEncodingQualityLevels*
347MediaProfiles::findImageEncodingQualityLevels(int cameraId) const
348{
349 int n = mImageEncodingQualityLevels.size();
350 for (int i = 0; i < n; i++) {
351 ImageEncodingQualityLevels *levels = mImageEncodingQualityLevels[i];
352 if (levels->mCameraId == cameraId) {
353 return levels;
354 }
355 }
356 return NULL;
357}
358
359void MediaProfiles::addImageEncodingQualityLevel(int cameraId, const char** atts)
James Dongf5a83852010-02-23 17:21:44 -0800360{
361 CHECK(!strcmp("quality", atts[0]));
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800362 int quality = atoi(atts[1]);
Glenn Kasten90bebef2012-01-27 15:24:38 -0800363 ALOGV("%s: cameraId=%d, quality=%d", __func__, cameraId, quality);
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800364 ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
365
366 if (levels == NULL) {
367 levels = new ImageEncodingQualityLevels();
368 levels->mCameraId = cameraId;
369 mImageEncodingQualityLevels.add(levels);
370 }
371
372 levels->mLevels.add(quality);
373}
374
375/*static*/ int
376MediaProfiles::getCameraId(const char** atts)
377{
378 if (!atts[0]) return 0; // default cameraId = 0
379 CHECK(!strcmp("cameraId", atts[0]));
James Dongf5a83852010-02-23 17:21:44 -0800380 return atoi(atts[1]);
381}
382
James Dong0f056292011-05-09 18:49:31 -0700383void MediaProfiles::addStartTimeOffset(int cameraId, const char** atts)
384{
Eric Laurentb1eb1a02012-10-22 17:44:24 -0700385 int offsetTimeMs = 1000;
James Dong0f056292011-05-09 18:49:31 -0700386 if (atts[2]) {
387 CHECK(!strcmp("startOffsetMs", atts[2]));
388 offsetTimeMs = atoi(atts[3]);
389 }
390
Steve Block3856b092011-10-20 11:56:00 +0100391 ALOGV("%s: cameraId=%d, offset=%d ms", __func__, cameraId, offsetTimeMs);
James Dong0f056292011-05-09 18:49:31 -0700392 mStartTimeOffsets.replaceValueFor(cameraId, offsetTimeMs);
393}
Hong Tengcabd5f82011-07-06 18:33:09 -0700394
James Dong1d7491b2010-01-19 17:45:38 -0800395/*static*/ void
396MediaProfiles::startElementHandler(void *userData, const char *name, const char **atts)
397{
398 MediaProfiles *profiles = (MediaProfiles *) userData;
399 if (strcmp("Video", name) == 0) {
400 createVideoCodec(atts, profiles);
401 } else if (strcmp("Audio", name) == 0) {
402 createAudioCodec(atts, profiles);
403 } else if (strcmp("VideoEncoderCap", name) == 0 &&
404 strcmp("true", atts[3]) == 0) {
405 profiles->mVideoEncoders.add(createVideoEncoderCap(atts));
406 } else if (strcmp("AudioEncoderCap", name) == 0 &&
407 strcmp("true", atts[3]) == 0) {
408 profiles->mAudioEncoders.add(createAudioEncoderCap(atts));
409 } else if (strcmp("VideoDecoderCap", name) == 0 &&
410 strcmp("true", atts[3]) == 0) {
411 profiles->mVideoDecoders.add(createVideoDecoderCap(atts));
412 } else if (strcmp("AudioDecoderCap", name) == 0 &&
413 strcmp("true", atts[3]) == 0) {
414 profiles->mAudioDecoders.add(createAudioDecoderCap(atts));
415 } else if (strcmp("EncoderOutputFileFormat", name) == 0) {
416 profiles->mEncoderOutputFileFormats.add(createEncoderOutputFileFormat(atts));
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800417 } else if (strcmp("CamcorderProfiles", name) == 0) {
418 profiles->mCurrentCameraId = getCameraId(atts);
James Dong0f056292011-05-09 18:49:31 -0700419 profiles->addStartTimeOffset(profiles->mCurrentCameraId, atts);
James Dong1d7491b2010-01-19 17:45:38 -0800420 } else if (strcmp("EncoderProfile", name) == 0) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800421 profiles->mCamcorderProfiles.add(
James Dong2a7e0a12011-02-28 21:07:39 -0800422 createCamcorderProfile(profiles->mCurrentCameraId, atts, profiles->mCameraIds));
James Dongf5a83852010-02-23 17:21:44 -0800423 } else if (strcmp("ImageEncoding", name) == 0) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800424 profiles->addImageEncodingQualityLevel(profiles->mCurrentCameraId, atts);
James Dong1d7491b2010-01-19 17:45:38 -0800425 }
426}
427
James Dong2a7e0a12011-02-28 21:07:39 -0800428static bool isCamcorderProfile(camcorder_quality quality) {
429 return quality >= CAMCORDER_QUALITY_LIST_START &&
430 quality <= CAMCORDER_QUALITY_LIST_END;
431}
432
433static bool isTimelapseProfile(camcorder_quality quality) {
434 return quality >= CAMCORDER_QUALITY_TIME_LAPSE_LIST_START &&
435 quality <= CAMCORDER_QUALITY_TIME_LAPSE_LIST_END;
436}
437
Zhijun Hee0790972014-07-23 15:17:26 -0700438static bool isHighSpeedProfile(camcorder_quality quality) {
439 return quality >= CAMCORDER_QUALITY_HIGH_SPEED_LIST_START &&
440 quality <= CAMCORDER_QUALITY_HIGH_SPEED_LIST_END;
441}
442
James Dong2a7e0a12011-02-28 21:07:39 -0800443void MediaProfiles::initRequiredProfileRefs(const Vector<int>& cameraIds) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700444 ALOGV("Number of camera ids: %zu", cameraIds.size());
James Dong2a7e0a12011-02-28 21:07:39 -0800445 CHECK(cameraIds.size() > 0);
446 mRequiredProfileRefs = new RequiredProfiles[cameraIds.size()];
447 for (size_t i = 0, n = cameraIds.size(); i < n; ++i) {
448 mRequiredProfileRefs[i].mCameraId = cameraIds[i];
449 for (size_t j = 0; j < kNumRequiredProfiles; ++j) {
450 mRequiredProfileRefs[i].mRefs[j].mHasRefProfile = false;
451 mRequiredProfileRefs[i].mRefs[j].mRefProfileIndex = -1;
452 if ((j & 1) == 0) { // low resolution
453 mRequiredProfileRefs[i].mRefs[j].mResolutionProduct = 0x7FFFFFFF;
454 } else { // high resolution
455 mRequiredProfileRefs[i].mRefs[j].mResolutionProduct = 0;
456 }
457 }
458 }
459}
460
461int MediaProfiles::getRequiredProfileRefIndex(int cameraId) {
462 for (size_t i = 0, n = mCameraIds.size(); i < n; ++i) {
463 if (mCameraIds[i] == cameraId) {
464 return i;
465 }
466 }
467 return -1;
468}
469
470void MediaProfiles::checkAndAddRequiredProfilesIfNecessary() {
471 if (sIsInitialized) {
472 return;
473 }
474
475 initRequiredProfileRefs(mCameraIds);
476
477 for (size_t i = 0, n = mCamcorderProfiles.size(); i < n; ++i) {
478 int product = mCamcorderProfiles[i]->mVideoCodec->mFrameWidth *
479 mCamcorderProfiles[i]->mVideoCodec->mFrameHeight;
480
481 camcorder_quality quality = mCamcorderProfiles[i]->mQuality;
482 int cameraId = mCamcorderProfiles[i]->mCameraId;
483 int index = -1;
484 int refIndex = getRequiredProfileRefIndex(cameraId);
485 CHECK(refIndex != -1);
486 RequiredProfileRefInfo *info;
487 camcorder_quality refQuality;
James Dong2a7e0a12011-02-28 21:07:39 -0800488
Zhijun Hee0790972014-07-23 15:17:26 -0700489 // Check high and low from either camcorder profile, timelapse profile
490 // or high speed profile, but not all of them. Default, check camcorder profile
James Dong2a7e0a12011-02-28 21:07:39 -0800491 size_t j = 0;
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200492 size_t o = 2;
James Dong2a7e0a12011-02-28 21:07:39 -0800493 if (isTimelapseProfile(quality)) {
494 // Check timelapse profile instead.
495 j = 2;
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200496 o = kNumRequiredProfiles;
Zhijun Hee0790972014-07-23 15:17:26 -0700497 } else if (isHighSpeedProfile(quality)) {
498 // Skip the check for high speed profile.
499 continue;
James Dong2a7e0a12011-02-28 21:07:39 -0800500 } else {
501 // Must be camcorder profile.
502 CHECK(isCamcorderProfile(quality));
503 }
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200504 for (; j < o; ++j) {
James Dong2a7e0a12011-02-28 21:07:39 -0800505 info = &(mRequiredProfileRefs[refIndex].mRefs[j]);
506 if ((j % 2 == 0 && product > info->mResolutionProduct) || // low
507 (j % 2 != 0 && product < info->mResolutionProduct)) { // high
508 continue;
509 }
510 switch (j) {
511 case 0:
512 refQuality = CAMCORDER_QUALITY_LOW;
513 break;
514 case 1:
515 refQuality = CAMCORDER_QUALITY_HIGH;
516 break;
517 case 2:
518 refQuality = CAMCORDER_QUALITY_TIME_LAPSE_LOW;
519 break;
520 case 3:
521 refQuality = CAMCORDER_QUALITY_TIME_LAPSE_HIGH;
522 break;
523 default:
524 CHECK(!"Should never reach here");
525 }
526
527 if (!info->mHasRefProfile) {
528 index = getCamcorderProfileIndex(cameraId, refQuality);
529 }
530 if (index == -1) {
531 // New high or low quality profile is found.
532 // Update its reference.
533 info->mHasRefProfile = true;
534 info->mRefProfileIndex = i;
535 info->mResolutionProduct = product;
536 }
537 }
538 }
539
540 for (size_t cameraId = 0; cameraId < mCameraIds.size(); ++cameraId) {
541 for (size_t j = 0; j < kNumRequiredProfiles; ++j) {
542 int refIndex = getRequiredProfileRefIndex(cameraId);
543 CHECK(refIndex != -1);
544 RequiredProfileRefInfo *info =
545 &mRequiredProfileRefs[refIndex].mRefs[j];
546
547 if (info->mHasRefProfile) {
548
549 CamcorderProfile *profile =
550 new CamcorderProfile(
551 *mCamcorderProfiles[info->mRefProfileIndex]);
552
553 // Overwrite the quality
554 switch (j % kNumRequiredProfiles) {
555 case 0:
556 profile->mQuality = CAMCORDER_QUALITY_LOW;
557 break;
558 case 1:
559 profile->mQuality = CAMCORDER_QUALITY_HIGH;
560 break;
561 case 2:
562 profile->mQuality = CAMCORDER_QUALITY_TIME_LAPSE_LOW;
563 break;
564 case 3:
565 profile->mQuality = CAMCORDER_QUALITY_TIME_LAPSE_HIGH;
566 break;
567 default:
568 CHECK(!"Should never come here");
569 }
570
571 int index = getCamcorderProfileIndex(cameraId, profile->mQuality);
572 if (index != -1) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700573 ALOGV("Profile quality %d for camera %zu already exists",
James Dong2a7e0a12011-02-28 21:07:39 -0800574 profile->mQuality, cameraId);
575 CHECK(index == refIndex);
576 continue;
577 }
578
579 // Insert the new profile
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700580 ALOGV("Add a profile: quality %d=>%d for camera %zu",
James Dong2a7e0a12011-02-28 21:07:39 -0800581 mCamcorderProfiles[info->mRefProfileIndex]->mQuality,
582 profile->mQuality, cameraId);
583
584 mCamcorderProfiles.add(profile);
585 }
586 }
587 }
588}
589
James Dong1d7491b2010-01-19 17:45:38 -0800590/*static*/ MediaProfiles*
591MediaProfiles::getInstance()
592{
Steve Block3856b092011-10-20 11:56:00 +0100593 ALOGV("getInstance");
James Dong1d7491b2010-01-19 17:45:38 -0800594 Mutex::Autolock lock(sLock);
595 if (!sIsInitialized) {
596 char value[PROPERTY_VALUE_MAX];
597 if (property_get("media.settings.xml", value, NULL) <= 0) {
Pawin Vongmasad7db05b2017-05-03 04:19:20 -0700598 const char* xmlFile = nullptr;
599 for (auto const& f : xmlFiles) {
600 if (checkXmlFile(f)) {
601 xmlFile = f;
602 break;
603 }
604 }
605 if (xmlFile == nullptr) {
606 ALOGW("Could not find a validated xml file. "
607 "Using the default instance instead.");
James Dong1d7491b2010-01-19 17:45:38 -0800608 sInstance = createDefaultInstance();
609 } else {
Pawin Vongmasad7db05b2017-05-03 04:19:20 -0700610 sInstance = createInstanceFromXmlFile(xmlFile);
James Dong1d7491b2010-01-19 17:45:38 -0800611 }
612 } else {
613 sInstance = createInstanceFromXmlFile(value);
614 }
James Dong2a7e0a12011-02-28 21:07:39 -0800615 CHECK(sInstance != NULL);
616 sInstance->checkAndAddRequiredProfilesIfNecessary();
617 sIsInitialized = true;
James Dong1d7491b2010-01-19 17:45:38 -0800618 }
619
620 return sInstance;
621}
622
623/*static*/ MediaProfiles::VideoEncoderCap*
624MediaProfiles::createDefaultH263VideoEncoderCap()
625{
626 return new MediaProfiles::VideoEncoderCap(
627 VIDEO_ENCODER_H263, 192000, 420000, 176, 352, 144, 288, 1, 20);
628}
629
630/*static*/ MediaProfiles::VideoEncoderCap*
631MediaProfiles::createDefaultM4vVideoEncoderCap()
632{
633 return new MediaProfiles::VideoEncoderCap(
634 VIDEO_ENCODER_MPEG_4_SP, 192000, 420000, 176, 352, 144, 288, 1, 20);
635}
636
637
638/*static*/ void
639MediaProfiles::createDefaultVideoEncoders(MediaProfiles *profiles)
640{
641 profiles->mVideoEncoders.add(createDefaultH263VideoEncoderCap());
642 profiles->mVideoEncoders.add(createDefaultM4vVideoEncoderCap());
643}
644
645/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700646MediaProfiles::createDefaultCamcorderTimeLapseQcifProfile(camcorder_quality quality)
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700647{
648 MediaProfiles::VideoCodec *videoCodec =
649 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 1000000, 176, 144, 20);
650
651 AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
652 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
653 profile->mCameraId = 0;
654 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700655 profile->mQuality = quality;
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700656 profile->mDuration = 60;
657 profile->mVideoCodec = videoCodec;
658 profile->mAudioCodec = audioCodec;
659 return profile;
660}
661
662/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700663MediaProfiles::createDefaultCamcorderTimeLapse480pProfile(camcorder_quality quality)
James Dong1d7491b2010-01-19 17:45:38 -0800664{
665 MediaProfiles::VideoCodec *videoCodec =
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700666 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 20000000, 720, 480, 20);
James Dong1d7491b2010-01-19 17:45:38 -0800667
668 AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800669 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
670 profile->mCameraId = 0;
James Dong1d7491b2010-01-19 17:45:38 -0800671 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700672 profile->mQuality = quality;
James Dong1d7491b2010-01-19 17:45:38 -0800673 profile->mDuration = 60;
674 profile->mVideoCodec = videoCodec;
675 profile->mAudioCodec = audioCodec;
676 return profile;
677}
678
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700679/*static*/ void
680MediaProfiles::createDefaultCamcorderTimeLapseLowProfiles(
681 MediaProfiles::CamcorderProfile **lowTimeLapseProfile,
682 MediaProfiles::CamcorderProfile **lowSpecificTimeLapseProfile) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800683 *lowTimeLapseProfile = createDefaultCamcorderTimeLapseQcifProfile(
684 CAMCORDER_QUALITY_TIME_LAPSE_LOW);
685 *lowSpecificTimeLapseProfile = createDefaultCamcorderTimeLapseQcifProfile(
686 CAMCORDER_QUALITY_TIME_LAPSE_QCIF);
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700687}
688
689/*static*/ void
690MediaProfiles::createDefaultCamcorderTimeLapseHighProfiles(
691 MediaProfiles::CamcorderProfile **highTimeLapseProfile,
692 MediaProfiles::CamcorderProfile **highSpecificTimeLapseProfile) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800693 *highTimeLapseProfile = createDefaultCamcorderTimeLapse480pProfile(
694 CAMCORDER_QUALITY_TIME_LAPSE_HIGH);
695 *highSpecificTimeLapseProfile = createDefaultCamcorderTimeLapse480pProfile(
696 CAMCORDER_QUALITY_TIME_LAPSE_480P);
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700697}
698
James Dong1d7491b2010-01-19 17:45:38 -0800699/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700700MediaProfiles::createDefaultCamcorderQcifProfile(camcorder_quality quality)
James Dong1d7491b2010-01-19 17:45:38 -0800701{
702 MediaProfiles::VideoCodec *videoCodec =
703 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 192000, 176, 144, 20);
704
705 MediaProfiles::AudioCodec *audioCodec =
706 new MediaProfiles::AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
707
708 MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800709 profile->mCameraId = 0;
James Dong1d7491b2010-01-19 17:45:38 -0800710 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700711 profile->mQuality = quality;
James Dong1d7491b2010-01-19 17:45:38 -0800712 profile->mDuration = 30;
713 profile->mVideoCodec = videoCodec;
714 profile->mAudioCodec = audioCodec;
715 return profile;
716}
717
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700718/*static*/ MediaProfiles::CamcorderProfile*
719MediaProfiles::createDefaultCamcorderCifProfile(camcorder_quality quality)
720{
721 MediaProfiles::VideoCodec *videoCodec =
722 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 360000, 352, 288, 20);
723
724 AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
725 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
726 profile->mCameraId = 0;
727 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
728 profile->mQuality = quality;
729 profile->mDuration = 60;
730 profile->mVideoCodec = videoCodec;
731 profile->mAudioCodec = audioCodec;
732 return profile;
733}
734
735/*static*/ void
736MediaProfiles::createDefaultCamcorderLowProfiles(
737 MediaProfiles::CamcorderProfile **lowProfile,
738 MediaProfiles::CamcorderProfile **lowSpecificProfile) {
739 *lowProfile = createDefaultCamcorderQcifProfile(CAMCORDER_QUALITY_LOW);
740 *lowSpecificProfile = createDefaultCamcorderQcifProfile(CAMCORDER_QUALITY_QCIF);
741}
742
743/*static*/ void
744MediaProfiles::createDefaultCamcorderHighProfiles(
745 MediaProfiles::CamcorderProfile **highProfile,
746 MediaProfiles::CamcorderProfile **highSpecificProfile) {
747 *highProfile = createDefaultCamcorderCifProfile(CAMCORDER_QUALITY_HIGH);
748 *highSpecificProfile = createDefaultCamcorderCifProfile(CAMCORDER_QUALITY_CIF);
749}
750
James Dong1d7491b2010-01-19 17:45:38 -0800751/*static*/ void
752MediaProfiles::createDefaultCamcorderProfiles(MediaProfiles *profiles)
753{
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700754 // low camcorder profiles.
755 MediaProfiles::CamcorderProfile *lowProfile, *lowSpecificProfile;
756 createDefaultCamcorderLowProfiles(&lowProfile, &lowSpecificProfile);
757 profiles->mCamcorderProfiles.add(lowProfile);
758 profiles->mCamcorderProfiles.add(lowSpecificProfile);
759
760 // high camcorder profiles.
761 MediaProfiles::CamcorderProfile* highProfile, *highSpecificProfile;
762 createDefaultCamcorderHighProfiles(&highProfile, &highSpecificProfile);
763 profiles->mCamcorderProfiles.add(highProfile);
764 profiles->mCamcorderProfiles.add(highSpecificProfile);
765
766 // low camcorder time lapse profiles.
767 MediaProfiles::CamcorderProfile *lowTimeLapseProfile, *lowSpecificTimeLapseProfile;
768 createDefaultCamcorderTimeLapseLowProfiles(&lowTimeLapseProfile, &lowSpecificTimeLapseProfile);
769 profiles->mCamcorderProfiles.add(lowTimeLapseProfile);
770 profiles->mCamcorderProfiles.add(lowSpecificTimeLapseProfile);
771
772 // high camcorder time lapse profiles.
773 MediaProfiles::CamcorderProfile *highTimeLapseProfile, *highSpecificTimeLapseProfile;
Glenn Kastenb187de12014-12-30 08:18:15 -0800774 createDefaultCamcorderTimeLapseHighProfiles(&highTimeLapseProfile,
775 &highSpecificTimeLapseProfile);
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700776 profiles->mCamcorderProfiles.add(highTimeLapseProfile);
777 profiles->mCamcorderProfiles.add(highSpecificTimeLapseProfile);
James Dong8031ec72011-03-16 14:09:50 -0700778
779 // For emulator and other legacy devices which does not have a
780 // media_profiles.xml file, We assume that the default camera id
781 // is 0 and that is the only camera available.
782 profiles->mCameraIds.push(0);
James Dong1d7491b2010-01-19 17:45:38 -0800783}
784
785/*static*/ void
786MediaProfiles::createDefaultAudioEncoders(MediaProfiles *profiles)
787{
788 profiles->mAudioEncoders.add(createDefaultAmrNBEncoderCap());
789}
790
791/*static*/ void
792MediaProfiles::createDefaultVideoDecoders(MediaProfiles *profiles)
793{
794 MediaProfiles::VideoDecoderCap *cap =
795 new MediaProfiles::VideoDecoderCap(VIDEO_DECODER_WMV);
796
797 profiles->mVideoDecoders.add(cap);
798}
799
800/*static*/ void
801MediaProfiles::createDefaultAudioDecoders(MediaProfiles *profiles)
802{
803 MediaProfiles::AudioDecoderCap *cap =
804 new MediaProfiles::AudioDecoderCap(AUDIO_DECODER_WMA);
805
806 profiles->mAudioDecoders.add(cap);
807}
808
809/*static*/ void
810MediaProfiles::createDefaultEncoderOutputFileFormats(MediaProfiles *profiles)
811{
812 profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_THREE_GPP);
813 profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_MPEG_4);
814}
815
816/*static*/ MediaProfiles::AudioEncoderCap*
817MediaProfiles::createDefaultAmrNBEncoderCap()
818{
819 return new MediaProfiles::AudioEncoderCap(
820 AUDIO_ENCODER_AMR_NB, 5525, 12200, 8000, 8000, 1, 1);
821}
822
James Dongf5a83852010-02-23 17:21:44 -0800823/*static*/ void
824MediaProfiles::createDefaultImageEncodingQualityLevels(MediaProfiles *profiles)
825{
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800826 ImageEncodingQualityLevels *levels = new ImageEncodingQualityLevels();
827 levels->mCameraId = 0;
828 levels->mLevels.add(70);
829 levels->mLevels.add(80);
830 levels->mLevels.add(90);
831 profiles->mImageEncodingQualityLevels.add(levels);
James Dongf5a83852010-02-23 17:21:44 -0800832}
833
James Dong1d7491b2010-01-19 17:45:38 -0800834/*static*/ MediaProfiles*
835MediaProfiles::createDefaultInstance()
836{
837 MediaProfiles *profiles = new MediaProfiles;
838 createDefaultCamcorderProfiles(profiles);
839 createDefaultVideoEncoders(profiles);
840 createDefaultAudioEncoders(profiles);
841 createDefaultVideoDecoders(profiles);
842 createDefaultAudioDecoders(profiles);
843 createDefaultEncoderOutputFileFormats(profiles);
James Dongf5a83852010-02-23 17:21:44 -0800844 createDefaultImageEncodingQualityLevels(profiles);
James Dong1d7491b2010-01-19 17:45:38 -0800845 return profiles;
846}
847
Pawin Vongmasad7db05b2017-05-03 04:19:20 -0700848bool MediaProfiles::checkXmlFile(const char* xmlFile) {
849 struct stat fStat;
850 return stat(xmlFile, &fStat) == 0 && S_ISREG(fStat.st_mode);
851 // TODO: Add validation
852}
853
James Dong1d7491b2010-01-19 17:45:38 -0800854/*static*/ MediaProfiles*
855MediaProfiles::createInstanceFromXmlFile(const char *xml)
856{
857 FILE *fp = NULL;
858 CHECK((fp = fopen(xml, "r")));
859
860 XML_Parser parser = ::XML_ParserCreate(NULL);
861 CHECK(parser != NULL);
862
863 MediaProfiles *profiles = new MediaProfiles();
864 ::XML_SetUserData(parser, profiles);
865 ::XML_SetElementHandler(parser, startElementHandler, NULL);
866
867 /*
868 FIXME:
869 expat is not compiled with -DXML_DTD. We don't have DTD parsing support.
870
871 if (!::XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS)) {
Steve Block29357bc2012-01-06 19:20:56 +0000872 ALOGE("failed to enable DTD support in the xml file");
James Dong1d7491b2010-01-19 17:45:38 -0800873 return UNKNOWN_ERROR;
874 }
875
876 */
877
878 const int BUFF_SIZE = 512;
879 for (;;) {
880 void *buff = ::XML_GetBuffer(parser, BUFF_SIZE);
881 if (buff == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000882 ALOGE("failed to in call to XML_GetBuffer()");
James Dong1d7491b2010-01-19 17:45:38 -0800883 delete profiles;
884 profiles = NULL;
885 goto exit;
886 }
887
888 int bytes_read = ::fread(buff, 1, BUFF_SIZE, fp);
889 if (bytes_read < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000890 ALOGE("failed in call to read");
James Dong1d7491b2010-01-19 17:45:38 -0800891 delete profiles;
892 profiles = NULL;
893 goto exit;
894 }
895
896 CHECK(::XML_ParseBuffer(parser, bytes_read, bytes_read == 0));
897
898 if (bytes_read == 0) break; // done parsing the xml file
899 }
900
901exit:
902 ::XML_ParserFree(parser);
903 ::fclose(fp);
James Dong1d7491b2010-01-19 17:45:38 -0800904 return profiles;
905}
906
907Vector<output_format> MediaProfiles::getOutputFileFormats() const
908{
909 return mEncoderOutputFileFormats; // copy out
910}
911
912Vector<video_encoder> MediaProfiles::getVideoEncoders() const
913{
914 Vector<video_encoder> encoders;
915 for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
916 encoders.add(mVideoEncoders[i]->mCodec);
917 }
918 return encoders; // copy out
919}
920
921int MediaProfiles::getVideoEncoderParamByName(const char *name, video_encoder codec) const
922{
Steve Block3856b092011-10-20 11:56:00 +0100923 ALOGV("getVideoEncoderParamByName: %s for codec %d", name, codec);
James Dong1d7491b2010-01-19 17:45:38 -0800924 int index = -1;
925 for (size_t i = 0, n = mVideoEncoders.size(); i < n; ++i) {
926 if (mVideoEncoders[i]->mCodec == codec) {
927 index = i;
928 break;
929 }
930 }
931 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +0000932 ALOGE("The given video encoder %d is not found", codec);
James Dong1d7491b2010-01-19 17:45:38 -0800933 return -1;
934 }
935
936 if (!strcmp("enc.vid.width.min", name)) return mVideoEncoders[index]->mMinFrameWidth;
937 if (!strcmp("enc.vid.width.max", name)) return mVideoEncoders[index]->mMaxFrameWidth;
938 if (!strcmp("enc.vid.height.min", name)) return mVideoEncoders[index]->mMinFrameHeight;
939 if (!strcmp("enc.vid.height.max", name)) return mVideoEncoders[index]->mMaxFrameHeight;
940 if (!strcmp("enc.vid.bps.min", name)) return mVideoEncoders[index]->mMinBitRate;
941 if (!strcmp("enc.vid.bps.max", name)) return mVideoEncoders[index]->mMaxBitRate;
942 if (!strcmp("enc.vid.fps.min", name)) return mVideoEncoders[index]->mMinFrameRate;
943 if (!strcmp("enc.vid.fps.max", name)) return mVideoEncoders[index]->mMaxFrameRate;
944
Steve Block29357bc2012-01-06 19:20:56 +0000945 ALOGE("The given video encoder param name %s is not found", name);
James Dong1d7491b2010-01-19 17:45:38 -0800946 return -1;
947}
Hong Tengcabd5f82011-07-06 18:33:09 -0700948
James Dong1d7491b2010-01-19 17:45:38 -0800949Vector<audio_encoder> MediaProfiles::getAudioEncoders() const
950{
951 Vector<audio_encoder> encoders;
952 for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
953 encoders.add(mAudioEncoders[i]->mCodec);
954 }
955 return encoders; // copy out
956}
957
958int MediaProfiles::getAudioEncoderParamByName(const char *name, audio_encoder codec) const
959{
Steve Block3856b092011-10-20 11:56:00 +0100960 ALOGV("getAudioEncoderParamByName: %s for codec %d", name, codec);
James Dong1d7491b2010-01-19 17:45:38 -0800961 int index = -1;
962 for (size_t i = 0, n = mAudioEncoders.size(); i < n; ++i) {
963 if (mAudioEncoders[i]->mCodec == codec) {
964 index = i;
965 break;
966 }
967 }
968 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +0000969 ALOGE("The given audio encoder %d is not found", codec);
James Dong1d7491b2010-01-19 17:45:38 -0800970 return -1;
971 }
972
973 if (!strcmp("enc.aud.ch.min", name)) return mAudioEncoders[index]->mMinChannels;
974 if (!strcmp("enc.aud.ch.max", name)) return mAudioEncoders[index]->mMaxChannels;
975 if (!strcmp("enc.aud.bps.min", name)) return mAudioEncoders[index]->mMinBitRate;
976 if (!strcmp("enc.aud.bps.max", name)) return mAudioEncoders[index]->mMaxBitRate;
977 if (!strcmp("enc.aud.hz.min", name)) return mAudioEncoders[index]->mMinSampleRate;
978 if (!strcmp("enc.aud.hz.max", name)) return mAudioEncoders[index]->mMaxSampleRate;
979
Steve Block29357bc2012-01-06 19:20:56 +0000980 ALOGE("The given audio encoder param name %s is not found", name);
James Dong1d7491b2010-01-19 17:45:38 -0800981 return -1;
982}
983
984Vector<video_decoder> MediaProfiles::getVideoDecoders() const
985{
986 Vector<video_decoder> decoders;
987 for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
988 decoders.add(mVideoDecoders[i]->mCodec);
989 }
990 return decoders; // copy out
991}
992
993Vector<audio_decoder> MediaProfiles::getAudioDecoders() const
994{
995 Vector<audio_decoder> decoders;
996 for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
997 decoders.add(mAudioDecoders[i]->mCodec);
998 }
999 return decoders; // copy out
1000}
1001
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001002int MediaProfiles::getCamcorderProfileIndex(int cameraId, camcorder_quality quality) const
James Dong1d7491b2010-01-19 17:45:38 -08001003{
James Dong1d7491b2010-01-19 17:45:38 -08001004 int index = -1;
1005 for (size_t i = 0, n = mCamcorderProfiles.size(); i < n; ++i) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001006 if (mCamcorderProfiles[i]->mCameraId == cameraId &&
1007 mCamcorderProfiles[i]->mQuality == quality) {
James Dong1d7491b2010-01-19 17:45:38 -08001008 index = i;
1009 break;
1010 }
1011 }
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001012 return index;
1013}
1014
1015int MediaProfiles::getCamcorderProfileParamByName(const char *name,
1016 int cameraId,
1017 camcorder_quality quality) const
1018{
Steve Block3856b092011-10-20 11:56:00 +01001019 ALOGV("getCamcorderProfileParamByName: %s for camera %d, quality %d",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001020 name, cameraId, quality);
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001021
1022 int index = getCamcorderProfileIndex(cameraId, quality);
James Dong1d7491b2010-01-19 17:45:38 -08001023 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +00001024 ALOGE("The given camcorder profile camera %d quality %d is not found",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001025 cameraId, quality);
James Dong1d7491b2010-01-19 17:45:38 -08001026 return -1;
1027 }
1028
James Dongf5a83852010-02-23 17:21:44 -08001029 if (!strcmp("duration", name)) return mCamcorderProfiles[index]->mDuration;
James Dong1d7491b2010-01-19 17:45:38 -08001030 if (!strcmp("file.format", name)) return mCamcorderProfiles[index]->mFileFormat;
1031 if (!strcmp("vid.codec", name)) return mCamcorderProfiles[index]->mVideoCodec->mCodec;
1032 if (!strcmp("vid.width", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameWidth;
1033 if (!strcmp("vid.height", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameHeight;
1034 if (!strcmp("vid.bps", name)) return mCamcorderProfiles[index]->mVideoCodec->mBitRate;
1035 if (!strcmp("vid.fps", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameRate;
1036 if (!strcmp("aud.codec", name)) return mCamcorderProfiles[index]->mAudioCodec->mCodec;
1037 if (!strcmp("aud.bps", name)) return mCamcorderProfiles[index]->mAudioCodec->mBitRate;
1038 if (!strcmp("aud.ch", name)) return mCamcorderProfiles[index]->mAudioCodec->mChannels;
1039 if (!strcmp("aud.hz", name)) return mCamcorderProfiles[index]->mAudioCodec->mSampleRate;
1040
Steve Block29357bc2012-01-06 19:20:56 +00001041 ALOGE("The given camcorder profile param id %d name %s is not found", cameraId, name);
James Dong1d7491b2010-01-19 17:45:38 -08001042 return -1;
1043}
1044
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001045bool MediaProfiles::hasCamcorderProfile(int cameraId, camcorder_quality quality) const
1046{
1047 return (getCamcorderProfileIndex(cameraId, quality) != -1);
1048}
1049
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001050Vector<int> MediaProfiles::getImageEncodingQualityLevels(int cameraId) const
James Dongf5a83852010-02-23 17:21:44 -08001051{
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001052 Vector<int> result;
1053 ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
1054 if (levels != NULL) {
1055 result = levels->mLevels; // copy out
1056 }
1057 return result;
James Dongf5a83852010-02-23 17:21:44 -08001058}
1059
James Dong0f056292011-05-09 18:49:31 -07001060int MediaProfiles::getStartTimeOffsetMs(int cameraId) const {
1061 int offsetTimeMs = -1;
1062 ssize_t index = mStartTimeOffsets.indexOfKey(cameraId);
1063 if (index >= 0) {
1064 offsetTimeMs = mStartTimeOffsets.valueFor(cameraId);
1065 }
Steve Block3856b092011-10-20 11:56:00 +01001066 ALOGV("offsetTime=%d ms and cameraId=%d", offsetTimeMs, cameraId);
James Dong0f056292011-05-09 18:49:31 -07001067 return offsetTimeMs;
1068}
1069
James Dong1d7491b2010-01-19 17:45:38 -08001070MediaProfiles::~MediaProfiles()
1071{
1072 CHECK("destructor should never be called" == 0);
1073#if 0
1074 for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
1075 delete mAudioEncoders[i];
1076 }
1077 mAudioEncoders.clear();
1078
1079 for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
1080 delete mVideoEncoders[i];
1081 }
1082 mVideoEncoders.clear();
1083
1084 for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
1085 delete mVideoDecoders[i];
1086 }
1087 mVideoDecoders.clear();
1088
1089 for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
1090 delete mAudioDecoders[i];
1091 }
1092 mAudioDecoders.clear();
1093
1094 for (size_t i = 0; i < mCamcorderProfiles.size(); ++i) {
1095 delete mCamcorderProfiles[i];
1096 }
1097 mCamcorderProfiles.clear();
1098#endif
1099}
1100} // namespace android