blob: 3869389613cabbb0fe06092a926cc5c52b4a2391 [file] [log] [blame]
James Dongc3711942010-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>
26#include <expat.h>
27#include <media/MediaProfiles.h>
28#include <media/stagefright/MediaDebug.h>
29
30namespace android {
31
32Mutex MediaProfiles::sLock;
33bool MediaProfiles::sIsInitialized = false;
34MediaProfiles *MediaProfiles::sInstance = NULL;
35
36const MediaProfiles::NameToTagMap MediaProfiles::sVideoEncoderNameMap[] = {
37 {"h263", VIDEO_ENCODER_H263},
38 {"h264", VIDEO_ENCODER_H264},
39 {"m4v", VIDEO_ENCODER_MPEG_4_SP}
40};
41
42const MediaProfiles::NameToTagMap MediaProfiles::sAudioEncoderNameMap[] = {
43 {"amrnb", AUDIO_ENCODER_AMR_NB},
44 {"amrwb", AUDIO_ENCODER_AMR_WB},
45 {"aac", AUDIO_ENCODER_AAC},
46};
47
48const MediaProfiles::NameToTagMap MediaProfiles::sFileFormatMap[] = {
49 {"3gp", OUTPUT_FORMAT_THREE_GPP},
50 {"mp4", OUTPUT_FORMAT_MPEG_4}
51};
52
53const MediaProfiles::NameToTagMap MediaProfiles::sVideoDecoderNameMap[] = {
54 {"wmv", VIDEO_DECODER_WMV}
55};
56
57const MediaProfiles::NameToTagMap MediaProfiles::sAudioDecoderNameMap[] = {
58 {"wma", AUDIO_DECODER_WMA}
59};
60
61const MediaProfiles::NameToTagMap MediaProfiles::sCamcorderQualityNameMap[] = {
62 {"high", CAMCORDER_QUALITY_HIGH},
63 {"low", CAMCORDER_QUALITY_LOW}
64};
65
66/*static*/ void
67MediaProfiles::logVideoCodec(const MediaProfiles::VideoCodec& codec)
68{
69 LOGV("video codec:");
70 LOGV("codec = %d", codec.mCodec);
71 LOGV("bit rate: %d", codec.mBitRate);
72 LOGV("frame width: %d", codec.mFrameWidth);
73 LOGV("frame height: %d", codec.mFrameHeight);
74 LOGV("frame rate: %d", codec.mFrameRate);
75}
76
77/*static*/ void
78MediaProfiles::logAudioCodec(const MediaProfiles::AudioCodec& codec)
79{
80 LOGV("audio codec:");
81 LOGV("codec = %d", codec.mCodec);
82 LOGV("bit rate: %d", codec.mBitRate);
83 LOGV("sample rate: %d", codec.mSampleRate);
84 LOGV("number of channels: %d", codec.mChannels);
85}
86
87/*static*/ void
88MediaProfiles::logVideoEncoderCap(const MediaProfiles::VideoEncoderCap& cap)
89{
90 LOGV("video encoder cap:");
91 LOGV("codec = %d", cap.mCodec);
92 LOGV("bit rate: min = %d and max = %d", cap.mMinBitRate, cap.mMaxBitRate);
93 LOGV("frame width: min = %d and max = %d", cap.mMinFrameWidth, cap.mMaxFrameWidth);
94 LOGV("frame height: min = %d and max = %d", cap.mMinFrameHeight, cap.mMaxFrameHeight);
95 LOGV("frame rate: min = %d and max = %d", cap.mMinFrameRate, cap.mMaxFrameRate);
96}
97
98/*static*/ void
99MediaProfiles::logAudioEncoderCap(const MediaProfiles::AudioEncoderCap& cap)
100{
101 LOGV("audio encoder cap:");
102 LOGV("codec = %d", cap.mCodec);
103 LOGV("bit rate: min = %d and max = %d", cap.mMinBitRate, cap.mMaxBitRate);
104 LOGV("sample rate: min = %d and max = %d", cap.mMinSampleRate, cap.mMaxSampleRate);
105 LOGV("number of channels: min = %d and max = %d", cap.mMinChannels, cap.mMaxChannels);
106}
107
108/*static*/ void
109MediaProfiles::logVideoDecoderCap(const MediaProfiles::VideoDecoderCap& cap)
110{
111 LOGV("video decoder cap:");
112 LOGV("codec = %d", cap.mCodec);
113}
114
115/*static*/ void
116MediaProfiles::logAudioDecoderCap(const MediaProfiles::AudioDecoderCap& cap)
117{
118 LOGV("audio codec cap:");
119 LOGV("codec = %d", cap.mCodec);
120}
121
122/*static*/ int
123MediaProfiles::findTagForName(const MediaProfiles::NameToTagMap *map, size_t nMappings, const char *name)
124{
125 int tag = -1;
126 for (size_t i = 0; i < nMappings; ++i) {
127 if (!strcmp(map[i].name, name)) {
128 tag = map[i].tag;
129 break;
130 }
131 }
132 return tag;
133}
134
135/*static*/ MediaProfiles::VideoCodec*
136MediaProfiles::createVideoCodec(const char **atts, MediaProfiles *profiles)
137{
138 CHECK(!strcmp("codec", atts[0]) &&
139 !strcmp("bitRate", atts[2]) &&
140 !strcmp("width", atts[4]) &&
141 !strcmp("height", atts[6]) &&
142 !strcmp("frameRate", atts[8]));
143
144 const size_t nMappings = sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
145 const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
146 CHECK(codec != -1);
147
148 MediaProfiles::VideoCodec *videoCodec =
149 new MediaProfiles::VideoCodec(static_cast<video_encoder>(codec),
150 atoi(atts[3]), atoi(atts[5]), atoi(atts[7]), atoi(atts[9]));
151 logVideoCodec(*videoCodec);
152
153 size_t nCamcorderProfiles;
154 CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
155 profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mVideoCodec = videoCodec;
156 return videoCodec;
157}
158
159/*static*/ MediaProfiles::AudioCodec*
160MediaProfiles::createAudioCodec(const char **atts, MediaProfiles *profiles)
161{
162 CHECK(!strcmp("codec", atts[0]) &&
163 !strcmp("bitRate", atts[2]) &&
164 !strcmp("sampleRate", atts[4]) &&
165 !strcmp("channels", atts[6]));
166 const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
167 const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
168 CHECK(codec != -1);
169
170 MediaProfiles::AudioCodec *audioCodec =
171 new MediaProfiles::AudioCodec(static_cast<audio_encoder>(codec),
172 atoi(atts[3]), atoi(atts[5]), atoi(atts[7]));
173 logAudioCodec(*audioCodec);
174
175 size_t nCamcorderProfiles;
176 CHECK((nCamcorderProfiles = profiles->mCamcorderProfiles.size()) >= 1);
177 profiles->mCamcorderProfiles[nCamcorderProfiles - 1]->mAudioCodec = audioCodec;
178 return audioCodec;
179}
180/*static*/ MediaProfiles::AudioDecoderCap*
181MediaProfiles::createAudioDecoderCap(const char **atts)
182{
183 CHECK(!strcmp("name", atts[0]) &&
184 !strcmp("enabled", atts[2]));
185
186 const size_t nMappings = sizeof(sAudioDecoderNameMap)/sizeof(sAudioDecoderNameMap[0]);
187 const int codec = findTagForName(sAudioDecoderNameMap, nMappings, atts[1]);
188 CHECK(codec != -1);
189
190 MediaProfiles::AudioDecoderCap *cap =
191 new MediaProfiles::AudioDecoderCap(static_cast<audio_decoder>(codec));
192 logAudioDecoderCap(*cap);
193 return cap;
194}
195
196/*static*/ MediaProfiles::VideoDecoderCap*
197MediaProfiles::createVideoDecoderCap(const char **atts)
198{
199 CHECK(!strcmp("name", atts[0]) &&
200 !strcmp("enabled", atts[2]));
201
202 const size_t nMappings = sizeof(sVideoDecoderNameMap)/sizeof(sVideoDecoderNameMap[0]);
203 const int codec = findTagForName(sVideoDecoderNameMap, nMappings, atts[1]);
204 CHECK(codec != -1);
205
206 MediaProfiles::VideoDecoderCap *cap =
207 new MediaProfiles::VideoDecoderCap(static_cast<video_decoder>(codec));
208 logVideoDecoderCap(*cap);
209 return cap;
210}
211
212/*static*/ MediaProfiles::VideoEncoderCap*
213MediaProfiles::createVideoEncoderCap(const char **atts)
214{
215 CHECK(!strcmp("name", atts[0]) &&
216 !strcmp("enabled", atts[2]) &&
217 !strcmp("minBitRate", atts[4]) &&
218 !strcmp("maxBitRate", atts[6]) &&
219 !strcmp("minFrameWidth", atts[8]) &&
220 !strcmp("maxFrameWidth", atts[10]) &&
221 !strcmp("minFrameHeight", atts[12]) &&
222 !strcmp("maxFrameHeight", atts[14]) &&
223 !strcmp("minFrameRate", atts[16]) &&
224 !strcmp("maxFrameRate", atts[18]));
225
226 const size_t nMappings = sizeof(sVideoEncoderNameMap)/sizeof(sVideoEncoderNameMap[0]);
227 const int codec = findTagForName(sVideoEncoderNameMap, nMappings, atts[1]);
228 CHECK(codec != -1);
229
230 MediaProfiles::VideoEncoderCap *cap =
231 new MediaProfiles::VideoEncoderCap(static_cast<video_encoder>(codec),
232 atoi(atts[5]), atoi(atts[7]), atoi(atts[9]), atoi(atts[11]), atoi(atts[13]),
233 atoi(atts[15]), atoi(atts[17]), atoi(atts[19]));
234 logVideoEncoderCap(*cap);
235 return cap;
236}
237
238/*static*/ MediaProfiles::AudioEncoderCap*
239MediaProfiles::createAudioEncoderCap(const char **atts)
240{
241 CHECK(!strcmp("name", atts[0]) &&
242 !strcmp("enabled", atts[2]) &&
243 !strcmp("minBitRate", atts[4]) &&
244 !strcmp("maxBitRate", atts[6]) &&
245 !strcmp("minSampleRate", atts[8]) &&
246 !strcmp("maxSampleRate", atts[10]) &&
247 !strcmp("minChannels", atts[12]) &&
248 !strcmp("maxChannels", atts[14]));
249
250 const size_t nMappings = sizeof(sAudioEncoderNameMap)/sizeof(sAudioEncoderNameMap[0]);
251 const int codec = findTagForName(sAudioEncoderNameMap, nMappings, atts[1]);
252 CHECK(codec != -1);
253
254 MediaProfiles::AudioEncoderCap *cap =
255 new MediaProfiles::AudioEncoderCap(static_cast<audio_encoder>(codec), atoi(atts[5]), atoi(atts[7]),
256 atoi(atts[9]), atoi(atts[11]), atoi(atts[13]),
257 atoi(atts[15]));
258 logAudioEncoderCap(*cap);
259 return cap;
260}
261
262/*static*/ output_format
263MediaProfiles::createEncoderOutputFileFormat(const char **atts)
264{
265 CHECK(!strcmp("name", atts[0]));
266
267 const size_t nMappings =sizeof(sFileFormatMap)/sizeof(sFileFormatMap[0]);
268 const int format = findTagForName(sFileFormatMap, nMappings, atts[1]);
269 CHECK(format != -1);
270
271 return static_cast<output_format>(format);
272}
273
274/*static*/ MediaProfiles::CamcorderProfile*
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800275MediaProfiles::createCamcorderProfile(int cameraId, const char **atts)
James Dongc3711942010-01-19 17:45:38 -0800276{
277 CHECK(!strcmp("quality", atts[0]) &&
278 !strcmp("fileFormat", atts[2]) &&
279 !strcmp("duration", atts[4]));
280
281 const size_t nProfileMappings = sizeof(sCamcorderQualityNameMap)/sizeof(sCamcorderQualityNameMap[0]);
282 const int quality = findTagForName(sCamcorderQualityNameMap, nProfileMappings, atts[1]);
283 CHECK(quality != -1);
284
285 const size_t nFormatMappings = sizeof(sFileFormatMap)/sizeof(sFileFormatMap[0]);
286 const int fileFormat = findTagForName(sFileFormatMap, nFormatMappings, atts[3]);
287 CHECK(fileFormat != -1);
288
289 MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800290 profile->mCameraId = cameraId;
James Dongc3711942010-01-19 17:45:38 -0800291 profile->mFileFormat = static_cast<output_format>(fileFormat);
292 profile->mQuality = static_cast<camcorder_quality>(quality);
293 profile->mDuration = atoi(atts[5]);
294 return profile;
295}
296
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800297MediaProfiles::ImageEncodingQualityLevels*
298MediaProfiles::findImageEncodingQualityLevels(int cameraId) const
299{
300 int n = mImageEncodingQualityLevels.size();
301 for (int i = 0; i < n; i++) {
302 ImageEncodingQualityLevels *levels = mImageEncodingQualityLevels[i];
303 if (levels->mCameraId == cameraId) {
304 return levels;
305 }
306 }
307 return NULL;
308}
309
310void MediaProfiles::addImageEncodingQualityLevel(int cameraId, const char** atts)
James Dong9b433f02010-02-23 17:21:44 -0800311{
312 CHECK(!strcmp("quality", atts[0]));
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800313 int quality = atoi(atts[1]);
314 LOGV("%s: cameraId=%d, quality=%d\n", __func__, cameraId, quality);
315 ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
316
317 if (levels == NULL) {
318 levels = new ImageEncodingQualityLevels();
319 levels->mCameraId = cameraId;
320 mImageEncodingQualityLevels.add(levels);
321 }
322
323 levels->mLevels.add(quality);
324}
325
326/*static*/ int
327MediaProfiles::getCameraId(const char** atts)
328{
329 if (!atts[0]) return 0; // default cameraId = 0
330 CHECK(!strcmp("cameraId", atts[0]));
James Dong9b433f02010-02-23 17:21:44 -0800331 return atoi(atts[1]);
332}
333
James Dongc3711942010-01-19 17:45:38 -0800334/*static*/ void
335MediaProfiles::startElementHandler(void *userData, const char *name, const char **atts)
336{
337 MediaProfiles *profiles = (MediaProfiles *) userData;
338 if (strcmp("Video", name) == 0) {
339 createVideoCodec(atts, profiles);
340 } else if (strcmp("Audio", name) == 0) {
341 createAudioCodec(atts, profiles);
342 } else if (strcmp("VideoEncoderCap", name) == 0 &&
343 strcmp("true", atts[3]) == 0) {
344 profiles->mVideoEncoders.add(createVideoEncoderCap(atts));
345 } else if (strcmp("AudioEncoderCap", name) == 0 &&
346 strcmp("true", atts[3]) == 0) {
347 profiles->mAudioEncoders.add(createAudioEncoderCap(atts));
348 } else if (strcmp("VideoDecoderCap", name) == 0 &&
349 strcmp("true", atts[3]) == 0) {
350 profiles->mVideoDecoders.add(createVideoDecoderCap(atts));
351 } else if (strcmp("AudioDecoderCap", name) == 0 &&
352 strcmp("true", atts[3]) == 0) {
353 profiles->mAudioDecoders.add(createAudioDecoderCap(atts));
354 } else if (strcmp("EncoderOutputFileFormat", name) == 0) {
355 profiles->mEncoderOutputFileFormats.add(createEncoderOutputFileFormat(atts));
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800356 } else if (strcmp("CamcorderProfiles", name) == 0) {
357 profiles->mCurrentCameraId = getCameraId(atts);
James Dongc3711942010-01-19 17:45:38 -0800358 } else if (strcmp("EncoderProfile", name) == 0) {
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800359 profiles->mCamcorderProfiles.add(
360 createCamcorderProfile(profiles->mCurrentCameraId, atts));
James Dong9b433f02010-02-23 17:21:44 -0800361 } else if (strcmp("ImageEncoding", name) == 0) {
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800362 profiles->addImageEncodingQualityLevel(profiles->mCurrentCameraId, atts);
James Dongc3711942010-01-19 17:45:38 -0800363 }
364}
365
366/*static*/ MediaProfiles*
367MediaProfiles::getInstance()
368{
369 LOGV("getInstance");
370 Mutex::Autolock lock(sLock);
371 if (!sIsInitialized) {
372 char value[PROPERTY_VALUE_MAX];
373 if (property_get("media.settings.xml", value, NULL) <= 0) {
374 const char *defaultXmlFile = "/etc/media_profiles.xml";
375 FILE *fp = fopen(defaultXmlFile, "r");
376 if (fp == NULL) {
377 LOGW("could not find media config xml file");
378 sInstance = createDefaultInstance();
379 } else {
380 fclose(fp); // close the file first.
381 sInstance = createInstanceFromXmlFile(defaultXmlFile);
382 }
383 } else {
384 sInstance = createInstanceFromXmlFile(value);
385 }
386 }
387
388 return sInstance;
389}
390
391/*static*/ MediaProfiles::VideoEncoderCap*
392MediaProfiles::createDefaultH263VideoEncoderCap()
393{
394 return new MediaProfiles::VideoEncoderCap(
395 VIDEO_ENCODER_H263, 192000, 420000, 176, 352, 144, 288, 1, 20);
396}
397
398/*static*/ MediaProfiles::VideoEncoderCap*
399MediaProfiles::createDefaultM4vVideoEncoderCap()
400{
401 return new MediaProfiles::VideoEncoderCap(
402 VIDEO_ENCODER_MPEG_4_SP, 192000, 420000, 176, 352, 144, 288, 1, 20);
403}
404
405
406/*static*/ void
407MediaProfiles::createDefaultVideoEncoders(MediaProfiles *profiles)
408{
409 profiles->mVideoEncoders.add(createDefaultH263VideoEncoderCap());
410 profiles->mVideoEncoders.add(createDefaultM4vVideoEncoderCap());
411}
412
413/*static*/ MediaProfiles::CamcorderProfile*
414MediaProfiles::createDefaultCamcorderHighProfile()
415{
416 MediaProfiles::VideoCodec *videoCodec =
417 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 360000, 352, 288, 20);
418
419 AudioCodec *audioCodec = new AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800420 CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
421 profile->mCameraId = 0;
James Dongc3711942010-01-19 17:45:38 -0800422 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
423 profile->mQuality = CAMCORDER_QUALITY_HIGH;
424 profile->mDuration = 60;
425 profile->mVideoCodec = videoCodec;
426 profile->mAudioCodec = audioCodec;
427 return profile;
428}
429
430/*static*/ MediaProfiles::CamcorderProfile*
431MediaProfiles::createDefaultCamcorderLowProfile()
432{
433 MediaProfiles::VideoCodec *videoCodec =
434 new MediaProfiles::VideoCodec(VIDEO_ENCODER_H263, 192000, 176, 144, 20);
435
436 MediaProfiles::AudioCodec *audioCodec =
437 new MediaProfiles::AudioCodec(AUDIO_ENCODER_AMR_NB, 12200, 8000, 1);
438
439 MediaProfiles::CamcorderProfile *profile = new MediaProfiles::CamcorderProfile;
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800440 profile->mCameraId = 0;
James Dongc3711942010-01-19 17:45:38 -0800441 profile->mFileFormat = OUTPUT_FORMAT_THREE_GPP;
442 profile->mQuality = CAMCORDER_QUALITY_LOW;
443 profile->mDuration = 30;
444 profile->mVideoCodec = videoCodec;
445 profile->mAudioCodec = audioCodec;
446 return profile;
447}
448
449/*static*/ void
450MediaProfiles::createDefaultCamcorderProfiles(MediaProfiles *profiles)
451{
452 profiles->mCamcorderProfiles.add(createDefaultCamcorderHighProfile());
453 profiles->mCamcorderProfiles.add(createDefaultCamcorderLowProfile());
454}
455
456/*static*/ void
457MediaProfiles::createDefaultAudioEncoders(MediaProfiles *profiles)
458{
459 profiles->mAudioEncoders.add(createDefaultAmrNBEncoderCap());
460}
461
462/*static*/ void
463MediaProfiles::createDefaultVideoDecoders(MediaProfiles *profiles)
464{
465 MediaProfiles::VideoDecoderCap *cap =
466 new MediaProfiles::VideoDecoderCap(VIDEO_DECODER_WMV);
467
468 profiles->mVideoDecoders.add(cap);
469}
470
471/*static*/ void
472MediaProfiles::createDefaultAudioDecoders(MediaProfiles *profiles)
473{
474 MediaProfiles::AudioDecoderCap *cap =
475 new MediaProfiles::AudioDecoderCap(AUDIO_DECODER_WMA);
476
477 profiles->mAudioDecoders.add(cap);
478}
479
480/*static*/ void
481MediaProfiles::createDefaultEncoderOutputFileFormats(MediaProfiles *profiles)
482{
483 profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_THREE_GPP);
484 profiles->mEncoderOutputFileFormats.add(OUTPUT_FORMAT_MPEG_4);
485}
486
487/*static*/ MediaProfiles::AudioEncoderCap*
488MediaProfiles::createDefaultAmrNBEncoderCap()
489{
490 return new MediaProfiles::AudioEncoderCap(
491 AUDIO_ENCODER_AMR_NB, 5525, 12200, 8000, 8000, 1, 1);
492}
493
James Dong9b433f02010-02-23 17:21:44 -0800494/*static*/ void
495MediaProfiles::createDefaultImageEncodingQualityLevels(MediaProfiles *profiles)
496{
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800497 ImageEncodingQualityLevels *levels = new ImageEncodingQualityLevels();
498 levels->mCameraId = 0;
499 levels->mLevels.add(70);
500 levels->mLevels.add(80);
501 levels->mLevels.add(90);
502 profiles->mImageEncodingQualityLevels.add(levels);
James Dong9b433f02010-02-23 17:21:44 -0800503}
504
James Dongc3711942010-01-19 17:45:38 -0800505/*static*/ MediaProfiles*
506MediaProfiles::createDefaultInstance()
507{
508 MediaProfiles *profiles = new MediaProfiles;
509 createDefaultCamcorderProfiles(profiles);
510 createDefaultVideoEncoders(profiles);
511 createDefaultAudioEncoders(profiles);
512 createDefaultVideoDecoders(profiles);
513 createDefaultAudioDecoders(profiles);
514 createDefaultEncoderOutputFileFormats(profiles);
James Dong9b433f02010-02-23 17:21:44 -0800515 createDefaultImageEncodingQualityLevels(profiles);
James Dongc3711942010-01-19 17:45:38 -0800516 sIsInitialized = true;
517 return profiles;
518}
519
520/*static*/ MediaProfiles*
521MediaProfiles::createInstanceFromXmlFile(const char *xml)
522{
523 FILE *fp = NULL;
524 CHECK((fp = fopen(xml, "r")));
525
526 XML_Parser parser = ::XML_ParserCreate(NULL);
527 CHECK(parser != NULL);
528
529 MediaProfiles *profiles = new MediaProfiles();
530 ::XML_SetUserData(parser, profiles);
531 ::XML_SetElementHandler(parser, startElementHandler, NULL);
532
533 /*
534 FIXME:
535 expat is not compiled with -DXML_DTD. We don't have DTD parsing support.
536
537 if (!::XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS)) {
538 LOGE("failed to enable DTD support in the xml file");
539 return UNKNOWN_ERROR;
540 }
541
542 */
543
544 const int BUFF_SIZE = 512;
545 for (;;) {
546 void *buff = ::XML_GetBuffer(parser, BUFF_SIZE);
547 if (buff == NULL) {
548 LOGE("failed to in call to XML_GetBuffer()");
549 delete profiles;
550 profiles = NULL;
551 goto exit;
552 }
553
554 int bytes_read = ::fread(buff, 1, BUFF_SIZE, fp);
555 if (bytes_read < 0) {
556 LOGE("failed in call to read");
557 delete profiles;
558 profiles = NULL;
559 goto exit;
560 }
561
562 CHECK(::XML_ParseBuffer(parser, bytes_read, bytes_read == 0));
563
564 if (bytes_read == 0) break; // done parsing the xml file
565 }
566
567exit:
568 ::XML_ParserFree(parser);
569 ::fclose(fp);
570 if (profiles) {
571 sIsInitialized = true;
572 }
573 return profiles;
574}
575
576Vector<output_format> MediaProfiles::getOutputFileFormats() const
577{
578 return mEncoderOutputFileFormats; // copy out
579}
580
581Vector<video_encoder> MediaProfiles::getVideoEncoders() const
582{
583 Vector<video_encoder> encoders;
584 for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
585 encoders.add(mVideoEncoders[i]->mCodec);
586 }
587 return encoders; // copy out
588}
589
590int MediaProfiles::getVideoEncoderParamByName(const char *name, video_encoder codec) const
591{
592 LOGV("getVideoEncoderParamByName: %s for codec %d", name, codec);
593 int index = -1;
594 for (size_t i = 0, n = mVideoEncoders.size(); i < n; ++i) {
595 if (mVideoEncoders[i]->mCodec == codec) {
596 index = i;
597 break;
598 }
599 }
600 if (index == -1) {
601 LOGE("The given video encoder %d is not found", codec);
602 return -1;
603 }
604
605 if (!strcmp("enc.vid.width.min", name)) return mVideoEncoders[index]->mMinFrameWidth;
606 if (!strcmp("enc.vid.width.max", name)) return mVideoEncoders[index]->mMaxFrameWidth;
607 if (!strcmp("enc.vid.height.min", name)) return mVideoEncoders[index]->mMinFrameHeight;
608 if (!strcmp("enc.vid.height.max", name)) return mVideoEncoders[index]->mMaxFrameHeight;
609 if (!strcmp("enc.vid.bps.min", name)) return mVideoEncoders[index]->mMinBitRate;
610 if (!strcmp("enc.vid.bps.max", name)) return mVideoEncoders[index]->mMaxBitRate;
611 if (!strcmp("enc.vid.fps.min", name)) return mVideoEncoders[index]->mMinFrameRate;
612 if (!strcmp("enc.vid.fps.max", name)) return mVideoEncoders[index]->mMaxFrameRate;
613
614 LOGE("The given video encoder param name %s is not found", name);
615 return -1;
616}
617
618Vector<audio_encoder> MediaProfiles::getAudioEncoders() const
619{
620 Vector<audio_encoder> encoders;
621 for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
622 encoders.add(mAudioEncoders[i]->mCodec);
623 }
624 return encoders; // copy out
625}
626
627int MediaProfiles::getAudioEncoderParamByName(const char *name, audio_encoder codec) const
628{
629 LOGV("getAudioEncoderParamByName: %s for codec %d", name, codec);
630 int index = -1;
631 for (size_t i = 0, n = mAudioEncoders.size(); i < n; ++i) {
632 if (mAudioEncoders[i]->mCodec == codec) {
633 index = i;
634 break;
635 }
636 }
637 if (index == -1) {
638 LOGE("The given audio encoder %d is not found", codec);
639 return -1;
640 }
641
642 if (!strcmp("enc.aud.ch.min", name)) return mAudioEncoders[index]->mMinChannels;
643 if (!strcmp("enc.aud.ch.max", name)) return mAudioEncoders[index]->mMaxChannels;
644 if (!strcmp("enc.aud.bps.min", name)) return mAudioEncoders[index]->mMinBitRate;
645 if (!strcmp("enc.aud.bps.max", name)) return mAudioEncoders[index]->mMaxBitRate;
646 if (!strcmp("enc.aud.hz.min", name)) return mAudioEncoders[index]->mMinSampleRate;
647 if (!strcmp("enc.aud.hz.max", name)) return mAudioEncoders[index]->mMaxSampleRate;
648
649 LOGE("The given audio encoder param name %s is not found", name);
650 return -1;
651}
652
653Vector<video_decoder> MediaProfiles::getVideoDecoders() const
654{
655 Vector<video_decoder> decoders;
656 for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
657 decoders.add(mVideoDecoders[i]->mCodec);
658 }
659 return decoders; // copy out
660}
661
662Vector<audio_decoder> MediaProfiles::getAudioDecoders() const
663{
664 Vector<audio_decoder> decoders;
665 for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
666 decoders.add(mAudioDecoders[i]->mCodec);
667 }
668 return decoders; // copy out
669}
670
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800671int MediaProfiles::getCamcorderProfileParamByName(const char *name,
672 int cameraId,
673 camcorder_quality quality) const
James Dongc3711942010-01-19 17:45:38 -0800674{
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800675 LOGV("getCamcorderProfileParamByName: %s for camera %d, quality %d",
676 name, cameraId, quality);
James Dongc3711942010-01-19 17:45:38 -0800677
678 int index = -1;
679 for (size_t i = 0, n = mCamcorderProfiles.size(); i < n; ++i) {
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800680 if (mCamcorderProfiles[i]->mCameraId == cameraId &&
681 mCamcorderProfiles[i]->mQuality == quality) {
James Dongc3711942010-01-19 17:45:38 -0800682 index = i;
683 break;
684 }
685 }
686 if (index == -1) {
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800687 LOGE("The given camcorder profile camera %d quality %d is not found",
688 cameraId, quality);
James Dongc3711942010-01-19 17:45:38 -0800689 return -1;
690 }
691
James Dong9b433f02010-02-23 17:21:44 -0800692 if (!strcmp("duration", name)) return mCamcorderProfiles[index]->mDuration;
James Dongc3711942010-01-19 17:45:38 -0800693 if (!strcmp("file.format", name)) return mCamcorderProfiles[index]->mFileFormat;
694 if (!strcmp("vid.codec", name)) return mCamcorderProfiles[index]->mVideoCodec->mCodec;
695 if (!strcmp("vid.width", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameWidth;
696 if (!strcmp("vid.height", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameHeight;
697 if (!strcmp("vid.bps", name)) return mCamcorderProfiles[index]->mVideoCodec->mBitRate;
698 if (!strcmp("vid.fps", name)) return mCamcorderProfiles[index]->mVideoCodec->mFrameRate;
699 if (!strcmp("aud.codec", name)) return mCamcorderProfiles[index]->mAudioCodec->mCodec;
700 if (!strcmp("aud.bps", name)) return mCamcorderProfiles[index]->mAudioCodec->mBitRate;
701 if (!strcmp("aud.ch", name)) return mCamcorderProfiles[index]->mAudioCodec->mChannels;
702 if (!strcmp("aud.hz", name)) return mCamcorderProfiles[index]->mAudioCodec->mSampleRate;
703
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800704 LOGE("The given camcorder profile param id %d name %s is not found", cameraId, name);
James Dongc3711942010-01-19 17:45:38 -0800705 return -1;
706}
707
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800708Vector<int> MediaProfiles::getImageEncodingQualityLevels(int cameraId) const
James Dong9b433f02010-02-23 17:21:44 -0800709{
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800710 Vector<int> result;
711 ImageEncodingQualityLevels *levels = findImageEncodingQualityLevels(cameraId);
712 if (levels != NULL) {
713 result = levels->mLevels; // copy out
714 }
715 return result;
James Dong9b433f02010-02-23 17:21:44 -0800716}
717
James Dongc3711942010-01-19 17:45:38 -0800718MediaProfiles::~MediaProfiles()
719{
720 CHECK("destructor should never be called" == 0);
721#if 0
722 for (size_t i = 0; i < mAudioEncoders.size(); ++i) {
723 delete mAudioEncoders[i];
724 }
725 mAudioEncoders.clear();
726
727 for (size_t i = 0; i < mVideoEncoders.size(); ++i) {
728 delete mVideoEncoders[i];
729 }
730 mVideoEncoders.clear();
731
732 for (size_t i = 0; i < mVideoDecoders.size(); ++i) {
733 delete mVideoDecoders[i];
734 }
735 mVideoDecoders.clear();
736
737 for (size_t i = 0; i < mAudioDecoders.size(); ++i) {
738 delete mAudioDecoders[i];
739 }
740 mAudioDecoders.clear();
741
742 for (size_t i = 0; i < mCamcorderProfiles.size(); ++i) {
743 delete mCamcorderProfiles[i];
744 }
745 mCamcorderProfiles.clear();
746#endif
747}
748} // namespace android