blob: d65dc51114b093b8396d20b2ec3f06a1008ef5d3 [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 Huberbc554952011-09-08 14:12:44 -070027#include "include/MPEG2PSExtractor.h"
Andreas Hubera557b242010-06-07 13:05:37 -070028#include "include/MPEG2TSExtractor.h"
Gloria Wangd5770912010-06-22 13:55:38 -070029#include "include/DRMExtractor.h"
Gloria Wangc2c22e72010-11-01 15:53:16 -070030#include "include/WVMExtractor.h"
Glenn Kastenf9f223e2011-01-13 11:17:00 -080031#include "include/FLACExtractor.h"
Gloria Wangc5b0abf2011-02-02 14:12:49 -080032#include "include/AACExtractor.h"
Andreas Huber57515f32009-10-23 09:55:10 -070033
Andreas Huber072f5242010-05-20 14:56:53 -070034#include "matroska/MatroskaExtractor.h"
35
Andreas Huberefdd0882010-08-25 11:09:41 -070036#include <media/stagefright/foundation/AMessage.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070037#include <media/stagefright/DataSource.h>
Andreas Hubere6c40962009-09-10 14:13:30 -070038#include <media/stagefright/MediaDefs.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070039#include <media/stagefright/MediaExtractor.h>
Andreas Huberaee3c632010-01-11 15:35:19 -080040#include <media/stagefright/MetaData.h>
Andreas Hubere46b7be2009-07-14 16:56:47 -070041#include <utils/String8.h>
42
43namespace android {
44
Andreas Huberaee3c632010-01-11 15:35:19 -080045sp<MetaData> MediaExtractor::getMetaData() {
46 return new MetaData;
47}
48
Andreas Huber62f7ffe2010-05-06 10:18:05 -070049uint32_t MediaExtractor::flags() const {
Andreas Huber10b9b3f2010-10-08 10:16:24 -070050 return CAN_SEEK_BACKWARD | CAN_SEEK_FORWARD | CAN_PAUSE | CAN_SEEK;
Andreas Huber62f7ffe2010-05-06 10:18:05 -070051}
52
Andreas Hubere46b7be2009-07-14 16:56:47 -070053// static
Andreas Huberbe06d262009-08-14 14:37:10 -070054sp<MediaExtractor> MediaExtractor::Create(
55 const sp<DataSource> &source, const char *mime) {
Andreas Huberefdd0882010-08-25 11:09:41 -070056 sp<AMessage> meta;
57
Andreas Hubere46b7be2009-07-14 16:56:47 -070058 String8 tmp;
59 if (mime == NULL) {
60 float confidence;
Andreas Huberefdd0882010-08-25 11:09:41 -070061 if (!source->sniff(&tmp, &confidence, &meta)) {
Steve Block71f2cf12011-10-20 11:56:00 +010062 ALOGV("FAILED to autodetect media content.");
Andreas Hubere46b7be2009-07-14 16:56:47 -070063
64 return NULL;
65 }
66
67 mime = tmp.string();
Steve Block71f2cf12011-10-20 11:56:00 +010068 ALOGV("Autodetected media content as '%s' with confidence %.2f",
Andreas Hubere46b7be2009-07-14 16:56:47 -070069 mime, confidence);
70 }
71
Glenn Kasten5f630692011-02-21 09:37:13 -080072 bool isDrm = false;
Glenn Kastendf0b6512011-02-18 15:42:25 -080073 // DRM MIME type syntax is "drm+type+original" where
74 // type is "es_based" or "container_based" and
75 // original is the content's cleartext MIME type
76 if (!strncmp(mime, "drm+", 4)) {
77 const char *originalMime = strchr(mime+4, '+');
78 if (originalMime == NULL) {
79 // second + not found
80 return NULL;
81 }
82 ++originalMime;
83 if (!strncmp(mime, "drm+es_based+", 13)) {
Glenn Kasten5f630692011-02-21 09:37:13 -080084 // DRMExtractor sets container metadata kKeyIsDRM to 1
Gloria Wangd5770912010-06-22 13:55:38 -070085 return new DRMExtractor(source, originalMime);
Glenn Kastendf0b6512011-02-18 15:42:25 -080086 } else if (!strncmp(mime, "drm+container_based+", 20)) {
Gloria Wangd5770912010-06-22 13:55:38 -070087 mime = originalMime;
Glenn Kasten5f630692011-02-21 09:37:13 -080088 isDrm = true;
Gloria Wangd5770912010-06-22 13:55:38 -070089 } else {
90 return NULL;
91 }
92 }
93
Glenn Kasten5f630692011-02-21 09:37:13 -080094 MediaExtractor *ret = NULL;
Andreas Hubere6c40962009-09-10 14:13:30 -070095 if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG4)
96 || !strcasecmp(mime, "audio/mp4")) {
Glenn Kasten5f630692011-02-21 09:37:13 -080097 ret = new MPEG4Extractor(source);
Andreas Hubere6c40962009-09-10 14:13:30 -070098 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_MPEG)) {
Glenn Kasten5f630692011-02-21 09:37:13 -080099 ret = new MP3Extractor(source, meta);
Andreas Hubere6c40962009-09-10 14:13:30 -0700100 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_NB)
101 || !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_WB)) {
Glenn Kasten5f630692011-02-21 09:37:13 -0800102 ret = new AMRExtractor(source);
Glenn Kastenf9f223e2011-01-13 11:17:00 -0800103 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_FLAC)) {
Glenn Kasten5f630692011-02-21 09:37:13 -0800104 ret = new FLACExtractor(source);
Andreas Huber6bce6d82009-11-03 16:00:58 -0800105 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WAV)) {
Glenn Kasten5f630692011-02-21 09:37:13 -0800106 ret = new WAVExtractor(source);
Andreas Huber388379f2010-05-07 10:35:13 -0700107 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_OGG)) {
Glenn Kasten5f630692011-02-21 09:37:13 -0800108 ret = new OggExtractor(source);
Andreas Huber072f5242010-05-20 14:56:53 -0700109 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MATROSKA)) {
Glenn Kasten5f630692011-02-21 09:37:13 -0800110 ret = new MatroskaExtractor(source);
Andreas Hubera557b242010-06-07 13:05:37 -0700111 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2TS)) {
Glenn Kasten5f630692011-02-21 09:37:13 -0800112 ret = new MPEG2TSExtractor(source);
Andreas Hubercabb7da2011-03-24 14:18:02 -0700113 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_AVI)) {
114 ret = new AVIExtractor(source);
Gloria Wangc2c22e72010-11-01 15:53:16 -0700115 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WVM)) {
Glenn Kasten5f630692011-02-21 09:37:13 -0800116 ret = new WVMExtractor(source);
James Dongaadbd802011-12-08 19:31:59 -0800117 if (ret != NULL) {
118 isDrm = true;
119 }
Gloria Wangc5b0abf2011-02-02 14:12:49 -0800120 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC_ADTS)) {
Andreas Huberfe5f0aa2011-11-01 11:01:01 -0700121 ret = new AACExtractor(source, meta);
Andreas Huberbc554952011-09-08 14:12:44 -0700122 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2PS)) {
123 ret = new MPEG2PSExtractor(source);
Glenn Kasten5f630692011-02-21 09:37:13 -0800124 }
Gloria Wang82428a82011-06-27 11:09:00 -0700125
126 if (ret != NULL) {
127 if (isDrm) {
128 ret->setDrmFlag(true);
129 } else {
130 ret->setDrmFlag(false);
131 }
Andreas Hubere46b7be2009-07-14 16:56:47 -0700132 }
133
Glenn Kasten5f630692011-02-21 09:37:13 -0800134 return ret;
Andreas Hubere46b7be2009-07-14 16:56:47 -0700135}
136
Andreas Hubere46b7be2009-07-14 16:56:47 -0700137} // namespace android