blob: af0131ea8111aabf92d9bc126009dc661e100128 [file] [log] [blame]
Andreas Hubere46b7be2009-07-14 16:56:47 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "MediaExtractor"
19#include <utils/Log.h>
20
Andreas Huber57515f32009-10-23 09:55:10 -070021#include "include/AMRExtractor.h"
Andreas Hubercabb7da2011-03-24 14:18:02 -070022#include "include/AVIExtractor.h"
Andreas Huber57515f32009-10-23 09:55:10 -070023#include "include/MP3Extractor.h"
24#include "include/MPEG4Extractor.h"
Andreas Huber6bce6d82009-11-03 16:00:58 -080025#include "include/WAVExtractor.h"
Andreas Huber388379f2010-05-07 10:35:13 -070026#include "include/OggExtractor.h"
Andreas Hubera557b242010-06-07 13:05:37 -070027#include "include/MPEG2TSExtractor.h"
Gloria Wangd5770912010-06-22 13:55:38 -070028#include "include/DRMExtractor.h"
Gloria Wangc2c22e72010-11-01 15:53:16 -070029#include "include/WVMExtractor.h"
Glenn Kastenf9f223e2011-01-13 11:17:00 -080030#include "include/FLACExtractor.h"
Gloria Wangc5b0abf2011-02-02 14:12:49 -080031#include "include/AACExtractor.h"
Andreas Huber57515f32009-10-23 09:55:10 -070032
Andreas Huber072f5242010-05-20 14:56:53 -070033#include "matroska/MatroskaExtractor.h"
34
Andreas Huberefdd0882010-08-25 11:09:41 -070035#include <media/stagefright/foundation/AMessage.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070036#include <media/stagefright/DataSource.h>
Andreas Hubere6c40962009-09-10 14:13:30 -070037#include <media/stagefright/MediaDefs.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070038#include <media/stagefright/MediaExtractor.h>
Andreas Huberaee3c632010-01-11 15:35:19 -080039#include <media/stagefright/MetaData.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070040#include <utils/String8.h>
41
42namespace android {
43
Andreas Huberaee3c632010-01-11 15:35:19 -080044sp<MetaData> MediaExtractor::getMetaData() {
45 return new MetaData;
46}
47
Andreas Huber62f7ffe2010-05-06 10:18:05 -070048uint32_t MediaExtractor::flags() const {
Andreas Huber10b9b3f2010-10-08 10:16:24 -070049 return CAN_SEEK_BACKWARD | CAN_SEEK_FORWARD | CAN_PAUSE | CAN_SEEK;
Andreas Huber62f7ffe2010-05-06 10:18:05 -070050}
51
Andreas Hubere46b7be2009-07-14 16:56:47 -070052// static
Andreas Huberbe06d262009-08-14 14:37:10 -070053sp<MediaExtractor> MediaExtractor::Create(
54 const sp<DataSource> &source, const char *mime) {
Andreas Huberefdd0882010-08-25 11:09:41 -070055 sp<AMessage> meta;
56
Andreas Hubere46b7be2009-07-14 16:56:47 -070057 String8 tmp;
58 if (mime == NULL) {
59 float confidence;
Andreas Huberefdd0882010-08-25 11:09:41 -070060 if (!source->sniff(&tmp, &confidence, &meta)) {
Andreas Huberaee3c632010-01-11 15:35:19 -080061 LOGV("FAILED to autodetect media content.");
Andreas Hubere46b7be2009-07-14 16:56:47 -070062
63 return NULL;
64 }
65
66 mime = tmp.string();
Andreas Huberad285432009-10-22 09:44:00 -070067 LOGV("Autodetected media content as '%s' with confidence %.2f",
Andreas Hubere46b7be2009-07-14 16:56:47 -070068 mime, confidence);
69 }
70
Glenn Kasten5f630692011-02-21 09:37:13 -080071 bool isDrm = false;
Glenn Kastendf0b6512011-02-18 15:42:25 -080072 // DRM MIME type syntax is "drm+type+original" where
73 // type is "es_based" or "container_based" and
74 // original is the content's cleartext MIME type
75 if (!strncmp(mime, "drm+", 4)) {
76 const char *originalMime = strchr(mime+4, '+');
77 if (originalMime == NULL) {
78 // second + not found
79 return NULL;
80 }
81 ++originalMime;
82 if (!strncmp(mime, "drm+es_based+", 13)) {
Glenn Kasten5f630692011-02-21 09:37:13 -080083 // DRMExtractor sets container metadata kKeyIsDRM to 1
Gloria Wangd5770912010-06-22 13:55:38 -070084 return new DRMExtractor(source, originalMime);
Glenn Kastendf0b6512011-02-18 15:42:25 -080085 } else if (!strncmp(mime, "drm+container_based+", 20)) {
Gloria Wangd5770912010-06-22 13:55:38 -070086 mime = originalMime;
Glenn Kasten5f630692011-02-21 09:37:13 -080087 isDrm = true;
Gloria Wangd5770912010-06-22 13:55:38 -070088 } else {
89 return NULL;
90 }
91 }
92
Glenn Kasten5f630692011-02-21 09:37:13 -080093 MediaExtractor *ret = NULL;
Andreas Hubere6c40962009-09-10 14:13:30 -070094 if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG4)
95 || !strcasecmp(mime, "audio/mp4")) {
Glenn Kasten5f630692011-02-21 09:37:13 -080096 ret = new MPEG4Extractor(source);
Andreas Hubere6c40962009-09-10 14:13:30 -070097 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_MPEG)) {
Glenn Kasten5f630692011-02-21 09:37:13 -080098 ret = new MP3Extractor(source, meta);
Andreas Hubere6c40962009-09-10 14:13:30 -070099 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_NB)
100 || !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_WB)) {
Glenn Kasten5f630692011-02-21 09:37:13 -0800101 ret = new AMRExtractor(source);
Glenn Kastenf9f223e2011-01-13 11:17:00 -0800102 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_FLAC)) {
Glenn Kasten5f630692011-02-21 09:37:13 -0800103 ret = new FLACExtractor(source);
Andreas Huber6bce6d82009-11-03 16:00:58 -0800104 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WAV)) {
Glenn Kasten5f630692011-02-21 09:37:13 -0800105 ret = new WAVExtractor(source);
Andreas Huber388379f2010-05-07 10:35:13 -0700106 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_OGG)) {
Glenn Kasten5f630692011-02-21 09:37:13 -0800107 ret = new OggExtractor(source);
Andreas Huber072f5242010-05-20 14:56:53 -0700108 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MATROSKA)) {
Glenn Kasten5f630692011-02-21 09:37:13 -0800109 ret = new MatroskaExtractor(source);
Andreas Hubera557b242010-06-07 13:05:37 -0700110 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2TS)) {
Glenn Kasten5f630692011-02-21 09:37:13 -0800111 ret = new MPEG2TSExtractor(source);
Andreas Hubercabb7da2011-03-24 14:18:02 -0700112 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_AVI)) {
113 ret = new AVIExtractor(source);
Gloria Wangc2c22e72010-11-01 15:53:16 -0700114 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WVM)) {
Glenn Kasten5f630692011-02-21 09:37:13 -0800115 ret = new WVMExtractor(source);
Gloria Wangc5b0abf2011-02-02 14:12:49 -0800116 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC_ADTS)) {
Glenn Kasten5f630692011-02-21 09:37:13 -0800117 ret = new AACExtractor(source);
118 }
119 if (ret != NULL && isDrm) {
120 ret->getMetaData()->setInt32(kKeyIsDRM, 1);
Andreas Hubere46b7be2009-07-14 16:56:47 -0700121 }
122
Glenn Kasten5f630692011-02-21 09:37:13 -0800123 return ret;
Andreas Hubere46b7be2009-07-14 16:56:47 -0700124}
125
Andreas Hubere46b7be2009-07-14 16:56:47 -0700126} // namespace android