blob: 98c54971ad998b1c95fe579d41a76ade70a9ca7f [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},
Ray Essickdf27b042018-11-27 18:55:09 -080051 {"aaceld", AUDIO_ENCODER_AAC_ELD},
52 {"opus", AUDIO_ENCODER_OPUS}
James Dong1d7491b2010-01-19 17:45:38 -080053};
54
55const MediaProfiles::NameToTagMap MediaProfiles::sFileFormatMap[] = {
56 {"3gp", OUTPUT_FORMAT_THREE_GPP},
57 {"mp4", OUTPUT_FORMAT_MPEG_4}
58};
59
60const MediaProfiles::NameToTagMap MediaProfiles::sVideoDecoderNameMap[] = {
61 {"wmv", VIDEO_DECODER_WMV}
62};
63
64const MediaProfiles::NameToTagMap MediaProfiles::sAudioDecoderNameMap[] = {
65 {"wma", AUDIO_DECODER_WMA}
66};
67
68const MediaProfiles::NameToTagMap MediaProfiles::sCamcorderQualityNameMap[] = {
Nipun Kwatrac0a84782010-09-06 15:59:02 -070069 {"low", CAMCORDER_QUALITY_LOW},
James Dong1d7491b2010-01-19 17:45:38 -080070 {"high", CAMCORDER_QUALITY_HIGH},
Nipun Kwatrac0a84782010-09-06 15:59:02 -070071 {"qcif", CAMCORDER_QUALITY_QCIF},
Nipun Kwatra9783ed82010-09-10 15:45:57 -070072 {"cif", CAMCORDER_QUALITY_CIF},
Nipun Kwatrac0a84782010-09-06 15:59:02 -070073 {"480p", CAMCORDER_QUALITY_480P},
74 {"720p", CAMCORDER_QUALITY_720P},
75 {"1080p", CAMCORDER_QUALITY_1080P},
Zhijun He5f6af1a2014-06-10 08:26:33 -070076 {"2160p", CAMCORDER_QUALITY_2160P},
James Dong669012d2011-09-19 16:27:31 -070077 {"qvga", CAMCORDER_QUALITY_QVGA},
Nipun Kwatrac0a84782010-09-06 15:59:02 -070078
79 {"timelapselow", CAMCORDER_QUALITY_TIME_LAPSE_LOW},
80 {"timelapsehigh", CAMCORDER_QUALITY_TIME_LAPSE_HIGH},
81 {"timelapseqcif", CAMCORDER_QUALITY_TIME_LAPSE_QCIF},
Nipun Kwatra9783ed82010-09-10 15:45:57 -070082 {"timelapsecif", CAMCORDER_QUALITY_TIME_LAPSE_CIF},
Nipun Kwatrac0a84782010-09-06 15:59:02 -070083 {"timelapse480p", CAMCORDER_QUALITY_TIME_LAPSE_480P},
84 {"timelapse720p", CAMCORDER_QUALITY_TIME_LAPSE_720P},
James Dong669012d2011-09-19 16:27:31 -070085 {"timelapse1080p", CAMCORDER_QUALITY_TIME_LAPSE_1080P},
Zhijun He5f6af1a2014-06-10 08:26:33 -070086 {"timelapse2160p", CAMCORDER_QUALITY_TIME_LAPSE_2160P},
James Dong669012d2011-09-19 16:27:31 -070087 {"timelapseqvga", CAMCORDER_QUALITY_TIME_LAPSE_QVGA},
Zhijun Hee0790972014-07-23 15:17:26 -070088
89 {"highspeedlow", CAMCORDER_QUALITY_HIGH_SPEED_LOW},
90 {"highspeedhigh", CAMCORDER_QUALITY_HIGH_SPEED_HIGH},
91 {"highspeed480p", CAMCORDER_QUALITY_HIGH_SPEED_480P},
92 {"highspeed720p", CAMCORDER_QUALITY_HIGH_SPEED_720P},
93 {"highspeed1080p", CAMCORDER_QUALITY_HIGH_SPEED_1080P},
Zhijun He9520aa62014-09-09 16:18:31 -070094 {"highspeed2160p", CAMCORDER_QUALITY_HIGH_SPEED_2160P},
Praveen Chavanb5bfa0e2019-01-16 15:49:42 -080095
96 // Vendor-specific profiles
97 {"vga", CAMCORDER_QUALITY_VGA},
98 {"4kdci", CAMCORDER_QUALITY_4KDCI},
99 {"timelapsevga", CAMCORDER_QUALITY_TIME_LAPSE_VGA},
100 {"timelapse4kdci", CAMCORDER_QUALITY_TIME_LAPSE_4KDCI},
101 {"highspeedcif", CAMCORDER_QUALITY_HIGH_SPEED_CIF},
102 {"highspeedvga", CAMCORDER_QUALITY_HIGH_SPEED_VGA},
103 {"highspeed4kdci", CAMCORDER_QUALITY_HIGH_SPEED_4KDCI},
104 {"qhd", CAMCORDER_QUALITY_QHD},
105 {"2k", CAMCORDER_QUALITY_2k},
106 {"timelapseqhd", CAMCORDER_QUALITY_TIME_LAPSE_QHD},
107 {"timelapse2k", CAMCORDER_QUALITY_TIME_LAPSE_2k},
James Dong1d7491b2010-01-19 17:45:38 -0800108};
109
Glenn Kasten80520382014-01-31 16:49:31 -0800110#if LOG_NDEBUG
111#define UNUSED __unused
112#else
113#define UNUSED
114#endif
115
James Dong1d7491b2010-01-19 17:45:38 -0800116/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800117MediaProfiles::logVideoCodec(const MediaProfiles::VideoCodec& codec UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800118{
Steve Block3856b092011-10-20 11:56:00 +0100119 ALOGV("video codec:");
120 ALOGV("codec = %d", codec.mCodec);
121 ALOGV("bit rate: %d", codec.mBitRate);
122 ALOGV("frame width: %d", codec.mFrameWidth);
123 ALOGV("frame height: %d", codec.mFrameHeight);
124 ALOGV("frame rate: %d", codec.mFrameRate);
James Dong1d7491b2010-01-19 17:45:38 -0800125}
126
127/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800128MediaProfiles::logAudioCodec(const MediaProfiles::AudioCodec& codec UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800129{
Steve Block3856b092011-10-20 11:56:00 +0100130 ALOGV("audio codec:");
131 ALOGV("codec = %d", codec.mCodec);
132 ALOGV("bit rate: %d", codec.mBitRate);
133 ALOGV("sample rate: %d", codec.mSampleRate);
134 ALOGV("number of channels: %d", codec.mChannels);
James Dong1d7491b2010-01-19 17:45:38 -0800135}
136
137/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800138MediaProfiles::logVideoEncoderCap(const MediaProfiles::VideoEncoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800139{
Steve Block3856b092011-10-20 11:56:00 +0100140 ALOGV("video encoder cap:");
141 ALOGV("codec = %d", cap.mCodec);
142 ALOGV("bit rate: min = %d and max = %d", cap.mMinBitRate, cap.mMaxBitRate);
143 ALOGV("frame width: min = %d and max = %d", cap.mMinFrameWidth, cap.mMaxFrameWidth);
144 ALOGV("frame height: min = %d and max = %d", cap.mMinFrameHeight, cap.mMaxFrameHeight);
145 ALOGV("frame rate: min = %d and max = %d", cap.mMinFrameRate, cap.mMaxFrameRate);
James Dong1d7491b2010-01-19 17:45:38 -0800146}
147
148/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800149MediaProfiles::logAudioEncoderCap(const MediaProfiles::AudioEncoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800150{
Steve Block3856b092011-10-20 11:56:00 +0100151 ALOGV("audio encoder cap:");
152 ALOGV("codec = %d", cap.mCodec);
153 ALOGV("bit rate: min = %d and max = %d", cap.mMinBitRate, cap.mMaxBitRate);
154 ALOGV("sample rate: min = %d and max = %d", cap.mMinSampleRate, cap.mMaxSampleRate);
155 ALOGV("number of channels: min = %d and max = %d", cap.mMinChannels, cap.mMaxChannels);
James Dong1d7491b2010-01-19 17:45:38 -0800156}
157
158/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800159MediaProfiles::logVideoDecoderCap(const MediaProfiles::VideoDecoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800160{
Steve Block3856b092011-10-20 11:56:00 +0100161 ALOGV("video decoder cap:");
162 ALOGV("codec = %d", cap.mCodec);
James Dong1d7491b2010-01-19 17:45:38 -0800163}
164
165/*static*/ void
Glenn Kasten80520382014-01-31 16:49:31 -0800166MediaProfiles::logAudioDecoderCap(const MediaProfiles::AudioDecoderCap& cap UNUSED)
James Dong1d7491b2010-01-19 17:45:38 -0800167{
Steve Block3856b092011-10-20 11:56:00 +0100168 ALOGV("audio codec cap:");
169 ALOGV("codec = %d", cap.mCodec);
James Dong1d7491b2010-01-19 17:45:38 -0800170}
171
172/*static*/ int
Glenn Kastenb187de12014-12-30 08:18:15 -0800173MediaProfiles::findTagForName(const MediaProfiles::NameToTagMap *map, size_t nMappings,
174 const char *name)
James Dong1d7491b2010-01-19 17:45:38 -0800175{
176 int tag = -1;
177 for (size_t i = 0; i < nMappings; ++i) {
178 if (!strcmp(map[i].name, name)) {
179 tag = map[i].tag;
180 break;
181 }
182 }
183 return tag;
184}
185
186/*static*/ MediaProfiles::VideoCodec*
187MediaProfiles::createVideoCodec(const char **atts, MediaProfiles *profiles)
188{
189 CHECK(!strcmp("codec", atts[0]) &&
190 !strcmp("bitRate", atts[2]) &&
191 !strcmp("width", atts[4]) &&
192 !strcmp("height", atts[6]) &&
193 !strcmp("frameRate", atts[8]));
194
195 const size_t nMappings = sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
196 const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
197 CHECK(codec != -1);
198
199 MediaProfiles::VideoCodec *videoCodec =
200 new MediaProfiles::VideoCodec(static_cast<video_encoder>(codec),
201 atoi(atts[3]), atoi(atts[5]), atoi(atts[7]), atoi(atts[9]));
202 logVideoCodec(*videoCodec);
203
204 size_t nCamcorderProfiles;
205 CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
206 profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mVideoCodec = videoCodec;
207 return videoCodec;
208}
209
210/*static*/ MediaProfiles::AudioCodec*
211MediaProfiles::createAudioCodec(const char **atts, MediaProfiles *profiles)
212{
213 CHECK(!strcmp("codec", atts[0]) &&
214 !strcmp("bitRate", atts[2]) &&
215 !strcmp("sampleRate", atts[4]) &&
216 !strcmp("channels", atts[6]));
217 const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
218 const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
219 CHECK(codec != -1);
220
221 MediaProfiles::AudioCodec *audioCodec =
222 new MediaProfiles::AudioCodec(static_cast<audio_encoder>(codec),
223 atoi(atts[3]), atoi(atts[5]), atoi(atts[7]));
224 logAudioCodec(*audioCodec);
225
226 size_t nCamcorderProfiles;
227 CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
228 profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mAudioCodec = audioCodec;
229 return audioCodec;
230}
231/*static*/ MediaProfiles::AudioDecoderCap*
232MediaProfiles::createAudioDecoderCap(const char **atts)
233{
234 CHECK(!strcmp("name", atts[0]) &&
235 !strcmp("enabled", atts[2]));
236
237 const size_t nMappings = sizeof(sAudioDecoderNameMap)/sizeof(sAudioDecoderNameMap[0]);
238 const int codec = findTagForName(sAudioDecoderNameMap, nMappings, atts[1]);
239 CHECK(codec != -1);
240
241 MediaProfiles::AudioDecoderCap *cap =
242 new MediaProfiles::AudioDecoderCap(static_cast<audio_decoder>(codec));
243 logAudioDecoderCap(*cap);
244 return cap;
245}
246
247/*static*/ MediaProfiles::VideoDecoderCap*
248MediaProfiles::createVideoDecoderCap(const char **atts)
249{
250 CHECK(!strcmp("name", atts[0]) &&
251 !strcmp("enabled", atts[2]));
252
253 const size_t nMappings = sizeof(sVideoDecoderNameMap)/sizeof(sVideoDecoderNameMap[0]);
254 const int codec = findTagForName(sVideoDecoderNameMap, nMappings, atts[1]);
255 CHECK(codec != -1);
256
257 MediaProfiles::VideoDecoderCap *cap =
258 new MediaProfiles::VideoDecoderCap(static_cast<video_decoder>(codec));
259 logVideoDecoderCap(*cap);
260 return cap;
261}
262
263/*static*/ MediaProfiles::VideoEncoderCap*
264MediaProfiles::createVideoEncoderCap(const char **atts)
265{
266 CHECK(!strcmp("name", atts[0]) &&
267 !strcmp("enabled", atts[2]) &&
268 !strcmp("minBitRate", atts[4]) &&
269 !strcmp("maxBitRate", atts[6]) &&
270 !strcmp("minFrameWidth", atts[8]) &&
271 !strcmp("maxFrameWidth", atts[10]) &&
272 !strcmp("minFrameHeight", atts[12]) &&
273 !strcmp("maxFrameHeight", atts[14]) &&
274 !strcmp("minFrameRate", atts[16]) &&
275 !strcmp("maxFrameRate", atts[18]));
276
277 const size_t nMappings = sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
278 const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
279 CHECK(codec != -1);
280
281 MediaProfiles::VideoEncoderCap *cap =
282 new MediaProfiles::VideoEncoderCap(static_cast<video_encoder>(codec),
283 atoi(atts[5]), atoi(atts[7]), atoi(atts[9]), atoi(atts[11]), atoi(atts[13]),
284 atoi(atts[15]), atoi(atts[17]), atoi(atts[19]));
285 logVideoEncoderCap(*cap);
286 return cap;
287}
288
289/*static*/ MediaProfiles::AudioEncoderCap*
290MediaProfiles::createAudioEncoderCap(const char **atts)
291{
292 CHECK(!strcmp("name", atts[0]) &&
293 !strcmp("enabled", atts[2]) &&
294 !strcmp("minBitRate", atts[4]) &&
295 !strcmp("maxBitRate", atts[6]) &&
296 !strcmp("minSampleRate", atts[8]) &&
297 !strcmp("maxSampleRate", atts[10]) &&
298 !strcmp("minChannels", atts[12]) &&
299 !strcmp("maxChannels", atts[14]));
300
301 const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
302 const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
303 CHECK(codec != -1);
304
305 MediaProfiles::AudioEncoderCap *cap =
Glenn Kastenb187de12014-12-30 08:18:15 -0800306 new MediaProfiles::AudioEncoderCap(static_cast<audio_encoder>(codec), atoi(atts[5]),
307 atoi(atts[7]), atoi(atts[9]), atoi(atts[11]), atoi(atts[13]), atoi(atts[15]));
James Dong1d7491b2010-01-19 17:45:38 -0800308 logAudioEncoderCap(*cap);
309 return cap;
310}
311
312/*static*/ output_format
313MediaProfiles::createEncoderOutputFileFormat(const char **atts)
314{
315 CHECK(!strcmp("name", atts[0]));
316
317 const size_t nMappings =sizeof(sFileFormatMap)/sizeof(sFileFormatMap[0]);
318 const int format = findTagForName(sFileFormatMap, nMappings, atts[1]);
319 CHECK(format != -1);
320
321 return static_cast<output_format>(format);
322}
323
James Dong2a7e0a12011-02-28 21:07:39 -0800324static bool isCameraIdFound(int cameraId, const Vector<int>& cameraIds) {
325 for (int i = 0, n = cameraIds.size(); i < n; ++i) {
326 if (cameraId == cameraIds[i]) {
327 return true;
328 }
329 }
330 return false;
331}
332
James Dong1d7491b2010-01-19 17:45:38 -0800333/*static*/ MediaProfiles::CamcorderProfile*
James Dong2a7e0a12011-02-28 21:07:39 -0800334MediaProfiles::createCamcorderProfile(int cameraId, const char **atts, Vector<int>& cameraIds)
James Dong1d7491b2010-01-19 17:45:38 -0800335{
336 CHECK(!strcmp("quality", atts[0]) &&
337 !strcmp("fileFormat", atts[2]) &&
338 !strcmp("duration", atts[4]));
339
Glenn Kastenb187de12014-12-30 08:18:15 -0800340 const size_t nProfileMappings = sizeof(sCamcorderQualityNameMap)/
341 sizeof(sCamcorderQualityNameMap[0]);
James Dong1d7491b2010-01-19 17:45:38 -0800342 const int quality = findTagForName(sCamcorderQualityNameMap, nProfileMappings, atts[1]);
343 CHECK(quality != -1);
344
345 const size_t nFormatMappings = sizeof(sFileFormatMap)/sizeof(sFileFormatMap[0]);
346 const int fileFormat = findTagForName(sFileFormatMap, nFormatMappings, atts[3]);
347 CHECK(fileFormat != -1);
348
349 MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800350 profile->mCameraId = cameraId;
James Dong2a7e0a12011-02-28 21:07:39 -0800351 if (!isCameraIdFound(cameraId, cameraIds)) {
352 cameraIds.add(cameraId);
353 }
James Dong1d7491b2010-01-19 17:45:38 -0800354 profile->mFileFormat = static_cast<output_format>(fileFormat);
355 profile->mQuality = static_cast<camcorder_quality>(quality);
356 profile->mDuration = atoi(atts[5]);
357 return profile;
358}
359
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800360MediaProfiles::ImageEncodingQualityLevels*
361MediaProfiles::findImageEncodingQualityLevels(int cameraId) const
362{
363 int n = mImageEncodingQualityLevels.size();
364 for (int i = 0; i < n; i++) {
365 ImageEncodingQualityLevels *levels = mImageEncodingQualityLevels[i];
366 if (levels->mCameraId == cameraId) {
367 return levels;
368 }
369 }
370 return NULL;
371}
372
373void MediaProfiles::addImageEncodingQualityLevel(int cameraId, const char** atts)
James Dongf5a83852010-02-23 17:21:44 -0800374{
375 CHECK(!strcmp("quality", atts[0]));
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800376 int quality = atoi(atts[1]);
Glenn Kasten90bebef2012-01-27 15:24:38 -0800377 ALOGV("%s: cameraId=%d, quality=%d", __func__, cameraId, quality);
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800378 ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
379
380 if (levels == NULL) {
381 levels = new ImageEncodingQualityLevels();
382 levels->mCameraId = cameraId;
383 mImageEncodingQualityLevels.add(levels);
384 }
385
386 levels->mLevels.add(quality);
387}
388
389/*static*/ int
390MediaProfiles::getCameraId(const char** atts)
391{
392 if (!atts[0]) return 0; // default cameraId = 0
393 CHECK(!strcmp("cameraId", atts[0]));
James Dongf5a83852010-02-23 17:21:44 -0800394 return atoi(atts[1]);
395}
396
James Dong0f056292011-05-09 18:49:31 -0700397void MediaProfiles::addStartTimeOffset(int cameraId, const char** atts)
398{
Eric Laurentb1eb1a02012-10-22 17:44:24 -0700399 int offsetTimeMs = 1000;
James Dong0f056292011-05-09 18:49:31 -0700400 if (atts[2]) {
401 CHECK(!strcmp("startOffsetMs", atts[2]));
402 offsetTimeMs = atoi(atts[3]);
403 }
404
Steve Block3856b092011-10-20 11:56:00 +0100405 ALOGV("%s: cameraId=%d, offset=%d ms", __func__, cameraId, offsetTimeMs);
James Dong0f056292011-05-09 18:49:31 -0700406 mStartTimeOffsets.replaceValueFor(cameraId, offsetTimeMs);
407}
Hong Tengcabd5f82011-07-06 18:33:09 -0700408
James Dong1d7491b2010-01-19 17:45:38 -0800409/*static*/ void
410MediaProfiles::startElementHandler(void *userData, const char *name, const char **atts)
411{
412 MediaProfiles *profiles = (MediaProfiles *) userData;
413 if (strcmp("Video", name) == 0) {
414 createVideoCodec(atts, profiles);
415 } else if (strcmp("Audio", name) == 0) {
416 createAudioCodec(atts, profiles);
417 } else if (strcmp("VideoEncoderCap", name) == 0 &&
418 strcmp("true", atts[3]) == 0) {
419 profiles->mVideoEncoders.add(createVideoEncoderCap(atts));
420 } else if (strcmp("AudioEncoderCap", name) == 0 &&
421 strcmp("true", atts[3]) == 0) {
422 profiles->mAudioEncoders.add(createAudioEncoderCap(atts));
423 } else if (strcmp("VideoDecoderCap", name) == 0 &&
424 strcmp("true", atts[3]) == 0) {
425 profiles->mVideoDecoders.add(createVideoDecoderCap(atts));
426 } else if (strcmp("AudioDecoderCap", name) == 0 &&
427 strcmp("true", atts[3]) == 0) {
428 profiles->mAudioDecoders.add(createAudioDecoderCap(atts));
429 } else if (strcmp("EncoderOutputFileFormat", name) == 0) {
430 profiles->mEncoderOutputFileFormats.add(createEncoderOutputFileFormat(atts));
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800431 } else if (strcmp("CamcorderProfiles", name) == 0) {
432 profiles->mCurrentCameraId = getCameraId(atts);
James Dong0f056292011-05-09 18:49:31 -0700433 profiles->addStartTimeOffset(profiles->mCurrentCameraId, atts);
James Dong1d7491b2010-01-19 17:45:38 -0800434 } else if (strcmp("EncoderProfile", name) == 0) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800435 profiles->mCamcorderProfiles.add(
James Dong2a7e0a12011-02-28 21:07:39 -0800436 createCamcorderProfile(profiles->mCurrentCameraId, atts, profiles->mCameraIds));
James Dongf5a83852010-02-23 17:21:44 -0800437 } else if (strcmp("ImageEncoding", name) == 0) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800438 profiles->addImageEncodingQualityLevel(profiles->mCurrentCameraId, atts);
James Dong1d7491b2010-01-19 17:45:38 -0800439 }
440}
441
James Dong2a7e0a12011-02-28 21:07:39 -0800442static bool isCamcorderProfile(camcorder_quality quality) {
443 return quality >= CAMCORDER_QUALITY_LIST_START &&
444 quality <= CAMCORDER_QUALITY_LIST_END;
445}
446
447static bool isTimelapseProfile(camcorder_quality quality) {
448 return quality >= CAMCORDER_QUALITY_TIME_LAPSE_LIST_START &&
449 quality <= CAMCORDER_QUALITY_TIME_LAPSE_LIST_END;
450}
451
Zhijun Hee0790972014-07-23 15:17:26 -0700452static bool isHighSpeedProfile(camcorder_quality quality) {
453 return quality >= CAMCORDER_QUALITY_HIGH_SPEED_LIST_START &&
454 quality <= CAMCORDER_QUALITY_HIGH_SPEED_LIST_END;
455}
456
James Dong2a7e0a12011-02-28 21:07:39 -0800457void MediaProfiles::initRequiredProfileRefs(const Vector<int>& cameraIds) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700458 ALOGV("Number of camera ids: %zu", cameraIds.size());
James Dong2a7e0a12011-02-28 21:07:39 -0800459 CHECK(cameraIds.size() > 0);
460 mRequiredProfileRefs = new RequiredProfiles[cameraIds.size()];
461 for (size_t i = 0, n = cameraIds.size(); i < n; ++i) {
462 mRequiredProfileRefs[i].mCameraId = cameraIds[i];
463 for (size_t j = 0; j < kNumRequiredProfiles; ++j) {
464 mRequiredProfileRefs[i].mRefs[j].mHasRefProfile = false;
465 mRequiredProfileRefs[i].mRefs[j].mRefProfileIndex = -1;
466 if ((j & 1) == 0) { // low resolution
467 mRequiredProfileRefs[i].mRefs[j].mResolutionProduct = 0x7FFFFFFF;
468 } else { // high resolution
469 mRequiredProfileRefs[i].mRefs[j].mResolutionProduct = 0;
470 }
471 }
472 }
473}
474
475int MediaProfiles::getRequiredProfileRefIndex(int cameraId) {
476 for (size_t i = 0, n = mCameraIds.size(); i < n; ++i) {
477 if (mCameraIds[i] == cameraId) {
478 return i;
479 }
480 }
481 return -1;
482}
483
484void MediaProfiles::checkAndAddRequiredProfilesIfNecessary() {
485 if (sIsInitialized) {
486 return;
487 }
488
489 initRequiredProfileRefs(mCameraIds);
490
491 for (size_t i = 0, n = mCamcorderProfiles.size(); i < n; ++i) {
492 int product = mCamcorderProfiles[i]->mVideoCodec->mFrameWidth *
493 mCamcorderProfiles[i]->mVideoCodec->mFrameHeight;
494
495 camcorder_quality quality = mCamcorderProfiles[i]->mQuality;
496 int cameraId = mCamcorderProfiles[i]->mCameraId;
497 int index = -1;
498 int refIndex = getRequiredProfileRefIndex(cameraId);
499 CHECK(refIndex != -1);
500 RequiredProfileRefInfo *info;
501 camcorder_quality refQuality;
James Dong2a7e0a12011-02-28 21:07:39 -0800502
Zhijun Hee0790972014-07-23 15:17:26 -0700503 // Check high and low from either camcorder profile, timelapse profile
504 // or high speed profile, but not all of them. Default, check camcorder profile
James Dong2a7e0a12011-02-28 21:07:39 -0800505 size_t j = 0;
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200506 size_t o = 2;
James Dong2a7e0a12011-02-28 21:07:39 -0800507 if (isTimelapseProfile(quality)) {
508 // Check timelapse profile instead.
509 j = 2;
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200510 o = kNumRequiredProfiles;
Zhijun Hee0790972014-07-23 15:17:26 -0700511 } else if (isHighSpeedProfile(quality)) {
512 // Skip the check for high speed profile.
513 continue;
James Dong2a7e0a12011-02-28 21:07:39 -0800514 } else {
515 // Must be camcorder profile.
516 CHECK(isCamcorderProfile(quality));
517 }
Bernhard Rosenkraenzer3c8889e2012-03-29 11:38:59 +0200518 for (; j < o; ++j) {
James Dong2a7e0a12011-02-28 21:07:39 -0800519 info = &(mRequiredProfileRefs[refIndex].mRefs[j]);
520 if ((j % 2 == 0 && product > info->mResolutionProduct) || // low
521 (j % 2 != 0 && product < info->mResolutionProduct)) { // high
522 continue;
523 }
524 switch (j) {
525 case 0:
526 refQuality = CAMCORDER_QUALITY_LOW;
527 break;
528 case 1:
529 refQuality = CAMCORDER_QUALITY_HIGH;
530 break;
531 case 2:
532 refQuality = CAMCORDER_QUALITY_TIME_LAPSE_LOW;
533 break;
534 case 3:
535 refQuality = CAMCORDER_QUALITY_TIME_LAPSE_HIGH;
536 break;
537 default:
538 CHECK(!"Should never reach here");
539 }
540
541 if (!info->mHasRefProfile) {
542 index = getCamcorderProfileIndex(cameraId, refQuality);
543 }
544 if (index == -1) {
545 // New high or low quality profile is found.
546 // Update its reference.
547 info->mHasRefProfile = true;
548 info->mRefProfileIndex = i;
549 info->mResolutionProduct = product;
550 }
551 }
552 }
553
554 for (size_t cameraId = 0; cameraId < mCameraIds.size(); ++cameraId) {
555 for (size_t j = 0; j < kNumRequiredProfiles; ++j) {
556 int refIndex = getRequiredProfileRefIndex(cameraId);
557 CHECK(refIndex != -1);
558 RequiredProfileRefInfo *info =
559 &mRequiredProfileRefs[refIndex].mRefs[j];
560
561 if (info->mHasRefProfile) {
562
George Burgess IV215545b2018-07-25 10:06:30 -0700563 std::unique_ptr<CamcorderProfile> profile =
564 std::make_unique<CamcorderProfile>(
James Dong2a7e0a12011-02-28 21:07:39 -0800565 *mCamcorderProfiles[info->mRefProfileIndex]);
566
567 // Overwrite the quality
568 switch (j % kNumRequiredProfiles) {
569 case 0:
570 profile->mQuality = CAMCORDER_QUALITY_LOW;
571 break;
572 case 1:
573 profile->mQuality = CAMCORDER_QUALITY_HIGH;
574 break;
575 case 2:
576 profile->mQuality = CAMCORDER_QUALITY_TIME_LAPSE_LOW;
577 break;
578 case 3:
579 profile->mQuality = CAMCORDER_QUALITY_TIME_LAPSE_HIGH;
580 break;
581 default:
582 CHECK(!"Should never come here");
583 }
584
585 int index = getCamcorderProfileIndex(cameraId, profile->mQuality);
586 if (index != -1) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700587 ALOGV("Profile quality %d for camera %zu already exists",
James Dong2a7e0a12011-02-28 21:07:39 -0800588 profile->mQuality, cameraId);
589 CHECK(index == refIndex);
590 continue;
591 }
592
593 // Insert the new profile
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700594 ALOGV("Add a profile: quality %d=>%d for camera %zu",
James Dong2a7e0a12011-02-28 21:07:39 -0800595 mCamcorderProfiles[info->mRefProfileIndex]->mQuality,
596 profile->mQuality, cameraId);
597
George Burgess IV215545b2018-07-25 10:06:30 -0700598 mCamcorderProfiles.add(profile.release());
James Dong2a7e0a12011-02-28 21:07:39 -0800599 }
600 }
601 }
602}
603
James Dong1d7491b2010-01-19 17:45:38 -0800604/*static*/ MediaProfiles*
605MediaProfiles::getInstance()
606{
Steve Block3856b092011-10-20 11:56:00 +0100607 ALOGV("getInstance");
James Dong1d7491b2010-01-19 17:45:38 -0800608 Mutex::Autolock lock(sLock);
609 if (!sIsInitialized) {
610 char value[PROPERTY_VALUE_MAX];
611 if (property_get("media.settings.xml", value, NULL) <= 0) {
Pawin Vongmasad7db05b2017-05-03 04:19:20 -0700612 const char* xmlFile = nullptr;
613 for (auto const& f : xmlFiles) {
614 if (checkXmlFile(f)) {
615 xmlFile = f;
616 break;
617 }
618 }
619 if (xmlFile == nullptr) {
620 ALOGW("Could not find a validated xml file. "
621 "Using the default instance instead.");
James Dong1d7491b2010-01-19 17:45:38 -0800622 sInstance = createDefaultInstance();
623 } else {
Pawin Vongmasad7db05b2017-05-03 04:19:20 -0700624 sInstance = createInstanceFromXmlFile(xmlFile);
James Dong1d7491b2010-01-19 17:45:38 -0800625 }
626 } else {
627 sInstance = createInstanceFromXmlFile(value);
628 }
James Dong2a7e0a12011-02-28 21:07:39 -0800629 CHECK(sInstance != NULL);
630 sInstance->checkAndAddRequiredProfilesIfNecessary();
631 sIsInitialized = true;
James Dong1d7491b2010-01-19 17:45:38 -0800632 }
633
634 return sInstance;
635}
636
637/*static*/ MediaProfiles::VideoEncoderCap*
638MediaProfiles::createDefaultH263VideoEncoderCap()
639{
640 return new MediaProfiles::VideoEncoderCap(
641 VIDEO_ENCODER_H263, 192000, 420000, 176, 352, 144, 288, 1, 20);
642}
643
644/*static*/ MediaProfiles::VideoEncoderCap*
645MediaProfiles::createDefaultM4vVideoEncoderCap()
646{
647 return new MediaProfiles::VideoEncoderCap(
648 VIDEO_ENCODER_MPEG_4_SP, 192000, 420000, 176, 352, 144, 288, 1, 20);
649}
650
651
652/*static*/ void
653MediaProfiles::createDefaultVideoEncoders(MediaProfiles *profiles)
654{
655 profiles->mVideoEncoders.add(createDefaultH263VideoEncoderCap());
656 profiles->mVideoEncoders.add(createDefaultM4vVideoEncoderCap());
657}
658
659/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700660MediaProfiles::createDefaultCamcorderTimeLapseQcifProfile(camcorder_quality quality)
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700661{
662 MediaProfiles::VideoCodec *videoCodec =
663 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 1000000, 176, 144, 20);
664
665 AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
666 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
667 profile->mCameraId = 0;
668 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700669 profile->mQuality = quality;
Nipun Kwatrac0a84782010-09-06 15:59:02 -0700670 profile->mDuration = 60;
671 profile->mVideoCodec = videoCodec;
672 profile->mAudioCodec = audioCodec;
673 return profile;
674}
675
676/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700677MediaProfiles::createDefaultCamcorderTimeLapse480pProfile(camcorder_quality quality)
James Dong1d7491b2010-01-19 17:45:38 -0800678{
679 MediaProfiles::VideoCodec *videoCodec =
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700680 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 20000000, 720, 480, 20);
James Dong1d7491b2010-01-19 17:45:38 -0800681
682 AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800683 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
684 profile->mCameraId = 0;
James Dong1d7491b2010-01-19 17:45:38 -0800685 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700686 profile->mQuality = quality;
James Dong1d7491b2010-01-19 17:45:38 -0800687 profile->mDuration = 60;
688 profile->mVideoCodec = videoCodec;
689 profile->mAudioCodec = audioCodec;
690 return profile;
691}
692
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700693/*static*/ void
694MediaProfiles::createDefaultCamcorderTimeLapseLowProfiles(
695 MediaProfiles::CamcorderProfile **lowTimeLapseProfile,
696 MediaProfiles::CamcorderProfile **lowSpecificTimeLapseProfile) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800697 *lowTimeLapseProfile = createDefaultCamcorderTimeLapseQcifProfile(
698 CAMCORDER_QUALITY_TIME_LAPSE_LOW);
699 *lowSpecificTimeLapseProfile = createDefaultCamcorderTimeLapseQcifProfile(
700 CAMCORDER_QUALITY_TIME_LAPSE_QCIF);
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700701}
702
703/*static*/ void
704MediaProfiles::createDefaultCamcorderTimeLapseHighProfiles(
705 MediaProfiles::CamcorderProfile **highTimeLapseProfile,
706 MediaProfiles::CamcorderProfile **highSpecificTimeLapseProfile) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800707 *highTimeLapseProfile = createDefaultCamcorderTimeLapse480pProfile(
708 CAMCORDER_QUALITY_TIME_LAPSE_HIGH);
709 *highSpecificTimeLapseProfile = createDefaultCamcorderTimeLapse480pProfile(
710 CAMCORDER_QUALITY_TIME_LAPSE_480P);
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700711}
712
James Dong1d7491b2010-01-19 17:45:38 -0800713/*static*/ MediaProfiles::CamcorderProfile*
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700714MediaProfiles::createDefaultCamcorderQcifProfile(camcorder_quality quality)
James Dong1d7491b2010-01-19 17:45:38 -0800715{
716 MediaProfiles::VideoCodec *videoCodec =
717 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 192000, 176, 144, 20);
718
719 MediaProfiles::AudioCodec *audioCodec =
720 new MediaProfiles::AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
721
722 MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800723 profile->mCameraId = 0;
James Dong1d7491b2010-01-19 17:45:38 -0800724 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700725 profile->mQuality = quality;
James Dong1d7491b2010-01-19 17:45:38 -0800726 profile->mDuration = 30;
727 profile->mVideoCodec = videoCodec;
728 profile->mAudioCodec = audioCodec;
729 return profile;
730}
731
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700732/*static*/ MediaProfiles::CamcorderProfile*
733MediaProfiles::createDefaultCamcorderCifProfile(camcorder_quality quality)
734{
735 MediaProfiles::VideoCodec *videoCodec =
736 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 360000, 352, 288, 20);
737
738 AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
739 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
740 profile->mCameraId = 0;
741 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
742 profile->mQuality = quality;
743 profile->mDuration = 60;
744 profile->mVideoCodec = videoCodec;
745 profile->mAudioCodec = audioCodec;
746 return profile;
747}
748
749/*static*/ void
750MediaProfiles::createDefaultCamcorderLowProfiles(
751 MediaProfiles::CamcorderProfile **lowProfile,
752 MediaProfiles::CamcorderProfile **lowSpecificProfile) {
753 *lowProfile = createDefaultCamcorderQcifProfile(CAMCORDER_QUALITY_LOW);
754 *lowSpecificProfile = createDefaultCamcorderQcifProfile(CAMCORDER_QUALITY_QCIF);
755}
756
757/*static*/ void
758MediaProfiles::createDefaultCamcorderHighProfiles(
759 MediaProfiles::CamcorderProfile **highProfile,
760 MediaProfiles::CamcorderProfile **highSpecificProfile) {
761 *highProfile = createDefaultCamcorderCifProfile(CAMCORDER_QUALITY_HIGH);
762 *highSpecificProfile = createDefaultCamcorderCifProfile(CAMCORDER_QUALITY_CIF);
763}
764
James Dong1d7491b2010-01-19 17:45:38 -0800765/*static*/ void
766MediaProfiles::createDefaultCamcorderProfiles(MediaProfiles *profiles)
767{
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700768 // low camcorder profiles.
769 MediaProfiles::CamcorderProfile *lowProfile, *lowSpecificProfile;
770 createDefaultCamcorderLowProfiles(&lowProfile, &lowSpecificProfile);
771 profiles->mCamcorderProfiles.add(lowProfile);
772 profiles->mCamcorderProfiles.add(lowSpecificProfile);
773
774 // high camcorder profiles.
775 MediaProfiles::CamcorderProfile* highProfile, *highSpecificProfile;
776 createDefaultCamcorderHighProfiles(&highProfile, &highSpecificProfile);
777 profiles->mCamcorderProfiles.add(highProfile);
778 profiles->mCamcorderProfiles.add(highSpecificProfile);
779
780 // low camcorder time lapse profiles.
781 MediaProfiles::CamcorderProfile *lowTimeLapseProfile, *lowSpecificTimeLapseProfile;
782 createDefaultCamcorderTimeLapseLowProfiles(&lowTimeLapseProfile, &lowSpecificTimeLapseProfile);
783 profiles->mCamcorderProfiles.add(lowTimeLapseProfile);
784 profiles->mCamcorderProfiles.add(lowSpecificTimeLapseProfile);
785
786 // high camcorder time lapse profiles.
787 MediaProfiles::CamcorderProfile *highTimeLapseProfile, *highSpecificTimeLapseProfile;
Glenn Kastenb187de12014-12-30 08:18:15 -0800788 createDefaultCamcorderTimeLapseHighProfiles(&highTimeLapseProfile,
789 &highSpecificTimeLapseProfile);
Nipun Kwatrad5672bc2010-09-16 22:25:23 -0700790 profiles->mCamcorderProfiles.add(highTimeLapseProfile);
791 profiles->mCamcorderProfiles.add(highSpecificTimeLapseProfile);
James Dong8031ec72011-03-16 14:09:50 -0700792
793 // For emulator and other legacy devices which does not have a
794 // media_profiles.xml file, We assume that the default camera id
795 // is 0 and that is the only camera available.
796 profiles->mCameraIds.push(0);
James Dong1d7491b2010-01-19 17:45:38 -0800797}
798
799/*static*/ void
800MediaProfiles::createDefaultAudioEncoders(MediaProfiles *profiles)
801{
802 profiles->mAudioEncoders.add(createDefaultAmrNBEncoderCap());
803}
804
805/*static*/ void
806MediaProfiles::createDefaultVideoDecoders(MediaProfiles *profiles)
807{
808 MediaProfiles::VideoDecoderCap *cap =
809 new MediaProfiles::VideoDecoderCap(VIDEO_DECODER_WMV);
810
811 profiles->mVideoDecoders.add(cap);
812}
813
814/*static*/ void
815MediaProfiles::createDefaultAudioDecoders(MediaProfiles *profiles)
816{
817 MediaProfiles::AudioDecoderCap *cap =
818 new MediaProfiles::AudioDecoderCap(AUDIO_DECODER_WMA);
819
820 profiles->mAudioDecoders.add(cap);
821}
822
823/*static*/ void
824MediaProfiles::createDefaultEncoderOutputFileFormats(MediaProfiles *profiles)
825{
826 profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_THREE_GPP);
827 profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_MPEG_4);
828}
829
830/*static*/ MediaProfiles::AudioEncoderCap*
831MediaProfiles::createDefaultAmrNBEncoderCap()
832{
833 return new MediaProfiles::AudioEncoderCap(
834 AUDIO_ENCODER_AMR_NB, 5525, 12200, 8000, 8000, 1, 1);
835}
836
James Dongf5a83852010-02-23 17:21:44 -0800837/*static*/ void
838MediaProfiles::createDefaultImageEncodingQualityLevels(MediaProfiles *profiles)
839{
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +0800840 ImageEncodingQualityLevels *levels = new ImageEncodingQualityLevels();
841 levels->mCameraId = 0;
842 levels->mLevels.add(70);
843 levels->mLevels.add(80);
844 levels->mLevels.add(90);
845 profiles->mImageEncodingQualityLevels.add(levels);
James Dongf5a83852010-02-23 17:21:44 -0800846}
847
James Dong1d7491b2010-01-19 17:45:38 -0800848/*static*/ MediaProfiles*
849MediaProfiles::createDefaultInstance()
850{
851 MediaProfiles *profiles = new MediaProfiles;
852 createDefaultCamcorderProfiles(profiles);
853 createDefaultVideoEncoders(profiles);
854 createDefaultAudioEncoders(profiles);
855 createDefaultVideoDecoders(profiles);
856 createDefaultAudioDecoders(profiles);
857 createDefaultEncoderOutputFileFormats(profiles);
James Dongf5a83852010-02-23 17:21:44 -0800858 createDefaultImageEncodingQualityLevels(profiles);
James Dong1d7491b2010-01-19 17:45:38 -0800859 return profiles;
860}
861
Pawin Vongmasad7db05b2017-05-03 04:19:20 -0700862bool MediaProfiles::checkXmlFile(const char* xmlFile) {
863 struct stat fStat;
864 return stat(xmlFile, &fStat) == 0 && S_ISREG(fStat.st_mode);
865 // TODO: Add validation
866}
867
James Dong1d7491b2010-01-19 17:45:38 -0800868/*static*/ MediaProfiles*
869MediaProfiles::createInstanceFromXmlFile(const char *xml)
870{
871 FILE *fp = NULL;
872 CHECK((fp = fopen(xml, "r")));
873
874 XML_Parser parser = ::XML_ParserCreate(NULL);
875 CHECK(parser != NULL);
876
877 MediaProfiles *profiles = new MediaProfiles();
878 ::XML_SetUserData(parser, profiles);
879 ::XML_SetElementHandler(parser, startElementHandler, NULL);
880
881 /*
882 FIXME:
883 expat is not compiled with -DXML_DTD. We don't have DTD parsing support.
884
885 if (!::XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS)) {
Steve Block29357bc2012-01-06 19:20:56 +0000886 ALOGE("failed to enable DTD support in the xml file");
James Dong1d7491b2010-01-19 17:45:38 -0800887 return UNKNOWN_ERROR;
888 }
889
890 */
891
892 const int BUFF_SIZE = 512;
893 for (;;) {
894 void *buff = ::XML_GetBuffer(parser, BUFF_SIZE);
895 if (buff == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000896 ALOGE("failed to in call to XML_GetBuffer()");
James Dong1d7491b2010-01-19 17:45:38 -0800897 delete profiles;
898 profiles = NULL;
899 goto exit;
900 }
901
902 int bytes_read = ::fread(buff, 1, BUFF_SIZE, fp);
903 if (bytes_read < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000904 ALOGE("failed in call to read");
James Dong1d7491b2010-01-19 17:45:38 -0800905 delete profiles;
906 profiles = NULL;
907 goto exit;
908 }
909
910 CHECK(::XML_ParseBuffer(parser, bytes_read, bytes_read == 0));
911
912 if (bytes_read == 0) break; // done parsing the xml file
913 }
914
915exit:
916 ::XML_ParserFree(parser);
917 ::fclose(fp);
James Dong1d7491b2010-01-19 17:45:38 -0800918 return profiles;
919}
920
921Vector<output_format> MediaProfiles::getOutputFileFormats() const
922{
923 return mEncoderOutputFileFormats; // copy out
924}
925
926Vector<video_encoder> MediaProfiles::getVideoEncoders() const
927{
928 Vector<video_encoder> encoders;
929 for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
930 encoders.add(mVideoEncoders[i]->mCodec);
931 }
932 return encoders; // copy out
933}
934
935int MediaProfiles::getVideoEncoderParamByName(const char *name, video_encoder codec) const
936{
Steve Block3856b092011-10-20 11:56:00 +0100937 ALOGV("getVideoEncoderParamByName: %s for codec %d", name, codec);
James Dong1d7491b2010-01-19 17:45:38 -0800938 int index = -1;
939 for (size_t i = 0, n = mVideoEncoders.size(); i < n; ++i) {
940 if (mVideoEncoders[i]->mCodec == codec) {
941 index = i;
942 break;
943 }
944 }
945 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +0000946 ALOGE("The given video encoder %d is not found", codec);
James Dong1d7491b2010-01-19 17:45:38 -0800947 return -1;
948 }
949
950 if (!strcmp("enc.vid.width.min", name)) return mVideoEncoders[index]->mMinFrameWidth;
951 if (!strcmp("enc.vid.width.max", name)) return mVideoEncoders[index]->mMaxFrameWidth;
952 if (!strcmp("enc.vid.height.min", name)) return mVideoEncoders[index]->mMinFrameHeight;
953 if (!strcmp("enc.vid.height.max", name)) return mVideoEncoders[index]->mMaxFrameHeight;
954 if (!strcmp("enc.vid.bps.min", name)) return mVideoEncoders[index]->mMinBitRate;
955 if (!strcmp("enc.vid.bps.max", name)) return mVideoEncoders[index]->mMaxBitRate;
956 if (!strcmp("enc.vid.fps.min", name)) return mVideoEncoders[index]->mMinFrameRate;
957 if (!strcmp("enc.vid.fps.max", name)) return mVideoEncoders[index]->mMaxFrameRate;
958
Steve Block29357bc2012-01-06 19:20:56 +0000959 ALOGE("The given video encoder param name %s is not found", name);
James Dong1d7491b2010-01-19 17:45:38 -0800960 return -1;
961}
Hong Tengcabd5f82011-07-06 18:33:09 -0700962
James Dong1d7491b2010-01-19 17:45:38 -0800963Vector<audio_encoder> MediaProfiles::getAudioEncoders() const
964{
965 Vector<audio_encoder> encoders;
966 for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
967 encoders.add(mAudioEncoders[i]->mCodec);
968 }
969 return encoders; // copy out
970}
971
972int MediaProfiles::getAudioEncoderParamByName(const char *name, audio_encoder codec) const
973{
Steve Block3856b092011-10-20 11:56:00 +0100974 ALOGV("getAudioEncoderParamByName: %s for codec %d", name, codec);
James Dong1d7491b2010-01-19 17:45:38 -0800975 int index = -1;
976 for (size_t i = 0, n = mAudioEncoders.size(); i < n; ++i) {
977 if (mAudioEncoders[i]->mCodec == codec) {
978 index = i;
979 break;
980 }
981 }
982 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +0000983 ALOGE("The given audio encoder %d is not found", codec);
James Dong1d7491b2010-01-19 17:45:38 -0800984 return -1;
985 }
986
987 if (!strcmp("enc.aud.ch.min", name)) return mAudioEncoders[index]->mMinChannels;
988 if (!strcmp("enc.aud.ch.max", name)) return mAudioEncoders[index]->mMaxChannels;
989 if (!strcmp("enc.aud.bps.min", name)) return mAudioEncoders[index]->mMinBitRate;
990 if (!strcmp("enc.aud.bps.max", name)) return mAudioEncoders[index]->mMaxBitRate;
991 if (!strcmp("enc.aud.hz.min", name)) return mAudioEncoders[index]->mMinSampleRate;
992 if (!strcmp("enc.aud.hz.max", name)) return mAudioEncoders[index]->mMaxSampleRate;
993
Steve Block29357bc2012-01-06 19:20:56 +0000994 ALOGE("The given audio encoder param name %s is not found", name);
James Dong1d7491b2010-01-19 17:45:38 -0800995 return -1;
996}
997
998Vector<video_decoder> MediaProfiles::getVideoDecoders() const
999{
1000 Vector<video_decoder> decoders;
1001 for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
1002 decoders.add(mVideoDecoders[i]->mCodec);
1003 }
1004 return decoders; // copy out
1005}
1006
1007Vector<audio_decoder> MediaProfiles::getAudioDecoders() const
1008{
1009 Vector<audio_decoder> decoders;
1010 for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
1011 decoders.add(mAudioDecoders[i]->mCodec);
1012 }
1013 return decoders; // copy out
1014}
1015
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001016int MediaProfiles::getCamcorderProfileIndex(int cameraId, camcorder_quality quality) const
James Dong1d7491b2010-01-19 17:45:38 -08001017{
James Dong1d7491b2010-01-19 17:45:38 -08001018 int index = -1;
1019 for (size_t i = 0, n = mCamcorderProfiles.size(); i < n; ++i) {
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001020 if (mCamcorderProfiles[i]->mCameraId == cameraId &&
1021 mCamcorderProfiles[i]->mQuality == quality) {
James Dong1d7491b2010-01-19 17:45:38 -08001022 index = i;
1023 break;
1024 }
1025 }
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001026 return index;
1027}
1028
1029int MediaProfiles::getCamcorderProfileParamByName(const char *name,
1030 int cameraId,
1031 camcorder_quality quality) const
1032{
Steve Block3856b092011-10-20 11:56:00 +01001033 ALOGV("getCamcorderProfileParamByName: %s for camera %d, quality %d",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001034 name, cameraId, quality);
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001035
1036 int index = getCamcorderProfileIndex(cameraId, quality);
James Dong1d7491b2010-01-19 17:45:38 -08001037 if (index == -1) {
Steve Block29357bc2012-01-06 19:20:56 +00001038 ALOGE("The given camcorder profile camera %d quality %d is not found",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001039 cameraId, quality);
James Dong1d7491b2010-01-19 17:45:38 -08001040 return -1;
1041 }
1042
James Dongf5a83852010-02-23 17:21:44 -08001043 if (!strcmp("duration", name)) return mCamcorderProfiles[index]->mDuration;
James Dong1d7491b2010-01-19 17:45:38 -08001044 if (!strcmp("file.format", name)) return mCamcorderProfiles[index]->mFileFormat;
1045 if (!strcmp("vid.codec", name)) return mCamcorderProfiles[index]->mVideoCodec->mCodec;
1046 if (!strcmp("vid.width", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameWidth;
1047 if (!strcmp("vid.height", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameHeight;
1048 if (!strcmp("vid.bps", name)) return mCamcorderProfiles[index]->mVideoCodec->mBitRate;
1049 if (!strcmp("vid.fps", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameRate;
1050 if (!strcmp("aud.codec", name)) return mCamcorderProfiles[index]->mAudioCodec->mCodec;
1051 if (!strcmp("aud.bps", name)) return mCamcorderProfiles[index]->mAudioCodec->mBitRate;
1052 if (!strcmp("aud.ch", name)) return mCamcorderProfiles[index]->mAudioCodec->mChannels;
1053 if (!strcmp("aud.hz", name)) return mCamcorderProfiles[index]->mAudioCodec->mSampleRate;
1054
Steve Block29357bc2012-01-06 19:20:56 +00001055 ALOGE("The given camcorder profile param id %d name %s is not found", cameraId, name);
James Dong1d7491b2010-01-19 17:45:38 -08001056 return -1;
1057}
1058
Nipun Kwatra8bb56032010-09-09 16:25:08 -07001059bool MediaProfiles::hasCamcorderProfile(int cameraId, camcorder_quality quality) const
1060{
1061 return (getCamcorderProfileIndex(cameraId, quality) != -1);
1062}
1063
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001064Vector<int> MediaProfiles::getImageEncodingQualityLevels(int cameraId) const
James Dongf5a83852010-02-23 17:21:44 -08001065{
Chih-Chung Chang3eaa4e92010-06-22 20:50:55 +08001066 Vector<int> result;
1067 ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
1068 if (levels != NULL) {
1069 result = levels->mLevels; // copy out
1070 }
1071 return result;
James Dongf5a83852010-02-23 17:21:44 -08001072}
1073
James Dong0f056292011-05-09 18:49:31 -07001074int MediaProfiles::getStartTimeOffsetMs(int cameraId) const {
1075 int offsetTimeMs = -1;
1076 ssize_t index = mStartTimeOffsets.indexOfKey(cameraId);
1077 if (index >= 0) {
1078 offsetTimeMs = mStartTimeOffsets.valueFor(cameraId);
1079 }
Steve Block3856b092011-10-20 11:56:00 +01001080 ALOGV("offsetTime=%d ms and cameraId=%d", offsetTimeMs, cameraId);
James Dong0f056292011-05-09 18:49:31 -07001081 return offsetTimeMs;
1082}
1083
James Dong1d7491b2010-01-19 17:45:38 -08001084MediaProfiles::~MediaProfiles()
1085{
1086 CHECK("destructor should never be called" == 0);
1087#if 0
1088 for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
1089 delete mAudioEncoders[i];
1090 }
1091 mAudioEncoders.clear();
1092
1093 for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
1094 delete mVideoEncoders[i];
1095 }
1096 mVideoEncoders.clear();
1097
1098 for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
1099 delete mVideoDecoders[i];
1100 }
1101 mVideoDecoders.clear();
1102
1103 for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
1104 delete mAudioDecoders[i];
1105 }
1106 mAudioDecoders.clear();
1107
1108 for (size_t i = 0; i < mCamcorderProfiles.size(); ++i) {
1109 delete mCamcorderProfiles[i];
1110 }
1111 mCamcorderProfiles.clear();
1112#endif
1113}
1114} // namespace android